/// <summary> /// Returns the UA COM Pseudo-servers registered on the local computer. /// </summary> public static List <ConfiguredEndpoint> Enumerate() { // enumerate server clsids. List <Guid> clsids = ConfigUtils.EnumClassesInCategory(ConfigUtils.CATID_PseudoComServers); // initialize server objects. List <ConfiguredEndpoint> servers = new List <ConfiguredEndpoint>(); for (int ii = 0; ii < clsids.Count; ii++) { ConfiguredEndpoint server = null; try { server = Load(clsids[ii]); servers.Add(server); } catch (Exception e) { Utils.Trace(e, "Unexpected error loading psuedo-server: {0}", clsids[ii]); continue; } // ensure the Pseudo-server points to the correct host process. Guid hostClsid = Guid.Empty; if (server.ComIdentity != null) { hostClsid = GetServerHostClsid(server.ComIdentity.Specification); } string hostPath = ConfigUtils.GetExecutablePath(hostClsid); if (String.IsNullOrEmpty(hostPath)) { continue; } RegistryKey key = Registry.ClassesRoot.OpenSubKey(String.Format(@"CLSID\{{{0}}}\LocalServer32", clsids[ii]), false); if (key != null) { if (hostPath != (string)key.GetValue(null, "")) { try { key.Close(); key = Registry.ClassesRoot.OpenSubKey( String.Format(@"CLSID\{{{0}}}\LocalServer32", clsids[ii]), true); key.SetValue(null, hostPath); } catch (Exception) { Utils.Trace("Could not update COM proxy server path for {0}.", server.ComIdentity.ProgId); } } key.Close(); } } return(servers); }
/// <summary> /// Saves the endpoint information in the registry. /// </summary> 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); } }