Opc.Ua.Configuration.PseudoComServer.Save C# (CSharp) Method

Save() public static method

Saves the endpoint information in the registry.
public static Save ( ConfiguredEndpoint endpoint ) : void
endpoint ConfiguredEndpoint
return void
		public static void Save(ConfiguredEndpoint endpoint)
		{
            if (endpoint == null) throw new ArgumentNullException("endpoint");

			if (endpoint.ComIdentity == null)
			{
				throw new ApplicationException("Endpoint does not have a COM identity specified.");
			}
            
            // choose the clsid for the host process.
            Guid hostClsid = GetServerHostClsid(endpoint.ComIdentity.Specification);
            
            // check of COM host process registered.
            string wrapperPath = ConfigUtils.GetExecutablePath(hostClsid);

            if (String.IsNullOrEmpty(wrapperPath))
            {
				throw new ApplicationException("The UA COM Host process is not registered on the machine.");
            }

			// verify prog id.
			string progId = endpoint.ComIdentity.ProgId;

			if (String.IsNullOrEmpty(progId))
			{
				throw new ApplicationException("Endpoint does not have a valid ProgId.");
			}

            // remove existing CLSID.
            Guid existingClsid = ConfigUtils.CLSIDFromProgID(progId);

            if (existingClsid != Guid.Empty)
            {
                ConfigUtils.UnregisterComServer(existingClsid);
            }

            // determine CLSID to use.
            Guid clsid = endpoint.ComIdentity.Clsid;

            if (clsid == Guid.Empty)
            {
                clsid = existingClsid;

                if (clsid == Guid.Empty)
                {
                    clsid = Guid.NewGuid();
                }

                endpoint.ComIdentity.Clsid = clsid;
            }
            
            // remove existing clsid.
            ConfigUtils.UnregisterComServer(clsid);

			string clsidKey = String.Format(@"CLSID\{{{0}}}", clsid.ToString().ToUpper());

			// create new entries.					
			RegistryKey key = Registry.ClassesRoot.CreateSubKey(clsidKey);
			
			if (key == null)
			{
				throw new ApplicationException("Could not create key: " + clsidKey);
			}

			// save description.
            if (endpoint.Description.Server.ApplicationName != null)
            {
                key.SetValue(null, endpoint.Description.Server.ApplicationName);
            }

			try
			{
				// create local server key.
				RegistryKey subkey = key.CreateSubKey("LocalServer32");

				if (subkey == null)
				{
					throw new ApplicationException("Could not create key: LocalServer32");
				}

				subkey.SetValue(null, wrapperPath);
				subkey.Close();

				// create prog id key.
				subkey = key.CreateSubKey("ProgId");

				if (subkey == null)
				{
					throw new ApplicationException("Could not create key: ProgId");
				}

				subkey.SetValue(null, progId);
				subkey.Close();
			}
			finally
			{
				key.Close();
			} 

            // save the configuration.
            SaveConfiguredEndpoint(clsid, endpoint);

			// create prog id key.
			key = Registry.ClassesRoot.CreateSubKey(progId);
			
			if (key == null)
			{
				throw new ApplicationException("Could not create key: " + progId);
			}

			try
			{	
				// create clsid key.
				RegistryKey subkey = key.CreateSubKey("CLSID");

				if (subkey == null)
				{
					throw new ApplicationException("Could not create key: CLSID");
				}

				subkey.SetValue(null, String.Format("{{{0}}}", clsid.ToString().ToUpper()));
				subkey.Close();

				// create the OPC key use with DA 2.0 servers.
                if (endpoint.ComIdentity.Specification == ComSpecification.DA)
                {
				    subkey = key.CreateSubKey("OPC");

				    if (subkey == null)
				    {
					    throw new ApplicationException("Could not create key: OPC");
				    }

				    subkey.Close();
                }
			}
			finally
			{
				key.Close();
			} 

			// register as wrapper server.
            ConfigUtils.RegisterClassInCategory(clsid, ConfigUtils.CATID_PseudoComServers, "OPC UA COM Pseudo-Servers");

            // register in OPC component categories.
            if (endpoint.ComIdentity.Specification == ComSpecification.DA)
            {
			    ConfigUtils.RegisterClassInCategory(clsid, ConfigUtils.CATID_OPCDAServer20);
#if !OPCUA_NODA3_SUPPORT
			    ConfigUtils.RegisterClassInCategory(clsid, ConfigUtils.CATID_OPCDAServer30);
#endif
            }
             
            else if (endpoint.ComIdentity.Specification == ComSpecification.AE)
            {
			    ConfigUtils.RegisterClassInCategory(clsid, ConfigUtils.CATID_OPCAEServer10);
            }
            
            else if (endpoint.ComIdentity.Specification == ComSpecification.HDA)
            {
			    ConfigUtils.RegisterClassInCategory(clsid, ConfigUtils.CATID_OPCHDAServer10);
            }
		}
         

Usage Example

コード例 #1
0
        private void OkBTN_Click(object sender, EventArgs e)
        {
            if (m_endpoint == null)
            {
                EditBTN_Click(sender, e);

                if (m_endpoint == null)
                {
                    return;
                }
            }

            try
            {
                EndpointComIdentity comIdentity = new PseudoComServerDlg().ShowDialog(m_endpoint);

                if (comIdentity == null)
                {
                    return;
                }

                m_endpoint.ComIdentity = comIdentity;

                // set a default configuration.
                if (comIdentity.Specification != ComSpecification.HDA)
                {
                    ComProxyConfiguration configuration = m_endpoint.ParseExtension <ComProxyConfiguration>(null);

                    if (configuration == null)
                    {
                        configuration = new ComProxyConfiguration();
                        configuration.BrowseBlockSize = 1000;
                    }

                    m_endpoint.UpdateExtension <ComProxyConfiguration>(null, configuration);
                }
                else
                {
                    ComHdaProxyConfiguration configuration = m_endpoint.ParseExtension <ComHdaProxyConfiguration>(null);

                    if (configuration == null)
                    {
                        configuration = new ComHdaProxyConfiguration();
                        configuration.BrowseBlockSize = 1000;
                        configuration.MaxReturnValues = 10000;
                    }

                    m_endpoint.UpdateExtension <ComHdaProxyConfiguration>(null, configuration);
                }

                PseudoComServer.Save(m_endpoint);

                DialogResult = DialogResult.OK;
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception);
            }
        }