/// <summary>
        /// Initializes the object from the registry.
        /// </summary>
        public RegisteredDotNetOpcServer(Guid clsid)
        {
            m_clsid        = clsid;
            m_progId       = ConfigUtils.ProgIDFromCLSID(clsid);
            m_description  = GetDescription(clsid);
            m_wrapperClsid = GetWrapper(clsid);
            m_serverClsid  = GetDotNetOpcServer(clsid);
            m_parameters   = GetParameters(clsid);

            if (m_description == m_progId)
            {
                m_description = null;
            }
        }
Пример #2
0
        /// <summary>
        /// Returns the wrapped objects registered on the local machine.
        /// </summary>
        public static List <DotNetOpcServer> EnumServers()
        {
            // enumerate clsids.
            List <Guid> clsids = ConfigUtils.EnumClassesInCategory(ConfigUtils.CATID_DotNetOpcServers);

            // initialize objects.
            List <DotNetOpcServer> servers = new List <DotNetOpcServer>();

            for (int ii = 0; ii < clsids.Count; ii++)
            {
                servers.Add(new DotNetOpcServer(clsids[ii]));
            }

            return(servers);
        }
Пример #3
0
        /// <summary>
        /// Sets the nodes in the control.
        /// </summary>
        public void Initialize(Guid catid)
        {
            Clear();

            m_catid = catid;

            List <Guid> clsids = ConfigUtils.EnumClassesInCategory(catid);

            foreach (Guid clsid in clsids)
            {
                AddItem(clsid);
            }

            AdjustColumns();
        }
Пример #4
0
        /// <summary>
        /// Registers the types in the specified assembly.
        /// </summary>
        public static void UnregisterAssembly(string filePath)
        {
            List <System.Type> types = ConfigUtils.UnregisterComTypes(filePath);

            foreach (System.Type type in types)
            {
                try
                {
                    ConfigUtils.UnregisterClassInCategory(type.GUID, ConfigUtils.CATID_DotNetOpcServers);
                }
                catch
                {
                    continue;
                }
            }
        }
Пример #5
0
        static void Main()
        {
            try
            {
                if (ProcessCommandLine())
                {
                    return;
                }

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new ComServerListDlg());
            }
            catch (Exception exception)
            {
                ConfigUtils.HandleException(Application.ProductName, MethodBase.GetCurrentMethod(), exception);
            }
        }
 /// <summary>
 /// Unregisters a COM server from the registry.
 /// </summary>
 public void Unregister()
 {
     ConfigUtils.UnregisterComServer(m_clsid);
 }
        /// <summary>
        /// Registers the endpoint as a COM server with the specified CLSID.
        /// </summary>
        public void Register()
        {
            DotNetOpcServer server = new DotNetOpcServer(m_serverClsid);

            if (server.Specifications == Specifications.None)
            {
                throw new ApplicationException("The .NET server does not implement any OPC specifications.");
            }

            DotNetOpcServerWrapper wrapper = new DotNetOpcServerWrapper(m_wrapperClsid);

            if (wrapper.Specifications == Specifications.None)
            {
                throw new ApplicationException("The .NET server wrapper does not implement any OPC interfaces.");
            }

            // determine the intersection of between the specs supported by the wrapper and the specs supported by the server.
            Specifications specifications = wrapper.Specifications & server.Specifications;

            if (specifications == Specifications.None)
            {
                throw new ApplicationException("The .NET server wrapper does not implement any OPC interfaces supported by the .NET server.");
            }

            // verify url and prog id.
            string progId = ProgId;

            if (m_wrapperClsid == Guid.Empty || String.IsNullOrEmpty(progId))
            {
                throw new ApplicationException("Proxy does not have a valid wrapper clsid or prog id.");
            }

            // verify wrapper path.
            string wrapperPath = ConfigUtils.GetExecutablePath(m_wrapperClsid);

            if (wrapperPath == null)
            {
                throw new ApplicationException("OPC server wrapper is not registered on this machine.");
            }

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

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

            string clsidKey = String.Format(@"CLSID\{{{0}}}", m_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 (String.IsNullOrEmpty(m_description))
            {
                m_description = progId;
            }

            key.SetValue(null, m_description);

            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.SetValue("WrapperClsid", String.Format("{{{0}}}", m_wrapperClsid));
                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();

                // create endpoint key.
                subkey = key.CreateSubKey(WrappedServerSubKey);

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

                // add parameters.
                try
                {
                    subkey.SetValue(null, String.Format("{{{0}}}", m_serverClsid));

                    // remove unused parameters.
                    foreach (string name in subkey.GetValueNames())
                    {
                        if (!String.IsNullOrEmpty(name) && !m_parameters.ContainsKey(name))
                        {
                            subkey.DeleteValue(name, false);
                        }
                    }

                    // add new parameters.
                    foreach (KeyValuePair <string, string> entry in m_parameters)
                    {
                        if (!String.IsNullOrEmpty(entry.Key))
                        {
                            subkey.SetValue(entry.Key, entry.Value);
                        }
                    }
                }
                finally
                {
                    subkey.Close();
                }
            }
            finally
            {
                key.Close();
            }

            // 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}}}", m_clsid.ToString().ToUpper()));
                subkey.Close();

                // create the OPC key use with DA 2.0 servers.
                if ((specifications & Specifications.DA2) != 0)
                {
                    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(m_clsid, ConfigUtils.CATID_RegisteredDotNetOpcServers, "OPC Wrapped COM Server Proxy");

            // register in OPC component categories.
            if ((specifications & Specifications.DA2) != 0)
            {
                ConfigUtils.RegisterClassInCategory(m_clsid, typeof(OpcRcw.Da.CATID_OPCDAServer20).GUID);
            }

            if ((specifications & Specifications.DA3) != 0)
            {
                ConfigUtils.RegisterClassInCategory(m_clsid, typeof(OpcRcw.Da.CATID_OPCDAServer30).GUID);
            }

            if ((specifications & Specifications.AE) != 0)
            {
                ConfigUtils.RegisterClassInCategory(m_clsid, typeof(OpcRcw.Ae.OPCEventServerCATID).GUID);
            }

            if ((specifications & Specifications.HDA) != 0)
            {
                ConfigUtils.RegisterClassInCategory(m_clsid, typeof(OpcRcw.Hda.CATID_OPCHDAServer10).GUID);
            }
        }
        /// <summary>
        /// Imports a server from a export file.
        /// </summary>
        private static RegisteredDotNetOpcServer Import(Export.RegisteredServer serverToImport)
        {
            RegisteredDotNetOpcServer server = new RegisteredDotNetOpcServer();

            // assign clsid if none specified.
            if (String.IsNullOrEmpty(serverToImport.Clsid))
            {
                server.Clsid = ConfigUtils.CLSIDFromProgID(serverToImport.ProgId);

                if (server.Clsid == Guid.Empty)
                {
                    server.Clsid = Guid.NewGuid();
                }
            }
            else
            {
                server.Clsid = new Guid(serverToImport.Clsid);
            }

            // get prog id and description.
            server.ProgId      = serverToImport.ProgId;
            server.Description = serverToImport.Description;

            // parse wrapper clsid/prog id.
            try
            {
                server.WrapperClsid = new Guid(serverToImport.WrapperClsid);
            }
            catch
            {
                server.WrapperClsid = ConfigUtils.CLSIDFromProgID(serverToImport.WrapperClsid);
            }

            // parse wrapped server clsid/prog id.
            try
            {
                server.ServerClsid = new Guid(serverToImport.ServerClsid);
            }
            catch
            {
                server.ServerClsid = ConfigUtils.CLSIDFromProgID(serverToImport.ServerClsid);
            }

            // read parameters.
            server.Parameters.Clear();

            if (!ConfigUtils.IsEmpty(serverToImport.Parameter))
            {
                for (int ii = 0; ii < serverToImport.Parameter.Length; ii++)
                {
                    Export.Parameter parameter = serverToImport.Parameter[ii];

                    if (parameter != null && !String.IsNullOrEmpty(parameter.Name))
                    {
                        server.Parameters.Add(parameter.Name, parameter.Value);
                    }
                }
            }

            // return new server.
            return(server);
        }
Пример #9
0
        /// <summary>
        /// Removes the registration for a COM server from the registry.
        /// </summary>
        public static void UnregisterComServer(Guid clsid)
        {
            // unregister class in categories.
            string categoriesKey = String.Format(@"CLSID\{{{0}}}\Implemented Categories", clsid);

            RegistryKey key = Registry.ClassesRoot.OpenSubKey(categoriesKey);

            if (key != null)
            {
                try
                {
                    foreach (string catid in key.GetSubKeyNames())
                    {
                        try
                        {
                            ConfigUtils.UnregisterClassInCategory(clsid, new Guid(catid.Substring(1, catid.Length - 2)));
                        }
                        catch (Exception)
                        {
                            // ignore errors.
                        }
                    }
                }
                finally
                {
                    key.Close();
                }
            }

            string progidKey = String.Format(@"CLSID\{{{0}}}\ProgId", clsid);

            // delete prog id.
            key = Registry.ClassesRoot.OpenSubKey(progidKey);

            if (key != null)
            {
                string progId = key.GetValue(null) as string;
                key.Close();

                if (!String.IsNullOrEmpty(progId))
                {
                    try
                    {
                        Registry.ClassesRoot.DeleteSubKeyTree(progId);
                    }
                    catch (Exception)
                    {
                        // ignore errors.
                    }
                }
            }

            // delete clsid.
            try
            {
                Registry.ClassesRoot.DeleteSubKeyTree(String.Format(@"CLSID\{{{0}}}", clsid));
            }
            catch (Exception)
            {
                // ignore errors.
            }
        }