Beispiel #1
0
        private void DotNetServerCB_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DotNetOpcServer server = DotNetServerCB.SelectedItem as DotNetOpcServer;

                if (server == null)
                {
                    return;
                }

                if (String.IsNullOrEmpty(ProgIdTB.Text))
                {
                    ProgIdTB.Text = server.ProgId + ".Wrapped";
                }

                if (String.IsNullOrEmpty(ClsidTB.Text))
                {
                    ClsidTB.Text = Guid.NewGuid().ToString();
                }

                // display the supported specifications.
                StringBuilder specifications = new StringBuilder();

                foreach (Specifications value in Enum.GetValues(typeof(Specifications)))
                {
                    if ((server.Specifications & value) != 0)
                    {
                        if (specifications.Length > 0)
                        {
                            specifications.Append(", ");
                        }

                        specifications.Append(value);
                    }
                }

                if (specifications.Length == 0)
                {
                    specifications.Append(Specifications.None);
                }

                SpecificationsTB.Text = specifications.ToString();
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
Beispiel #2
0
        private void BrowseBTN_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                // select file.
                OpenFileDialog dialog = new OpenFileDialog();

                dialog.CheckFileExists  = true;
                dialog.CheckPathExists  = true;
                dialog.DefaultExt       = ".dll";
                dialog.Filter           = ".NET Assemblies (*.dll)|*.dll|All Files (*.*)|*.*";
                dialog.ValidateNames    = true;
                dialog.Title            = "Load .NET Assembly";
                dialog.RestoreDirectory = true;
                dialog.AddExtension     = true;
                dialog.FileName         = "";
                dialog.InitialDirectory = m_currentDirectory;

                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                DotNetOpcServer.RegisterAssembly(dialog.FileName);
                Initialize();

                if (DotNetServerCB.SelectedIndex == -1 && DotNetServerCB.Items.Count > 0)
                {
                    DotNetServerCB.SelectedIndex = 0;
                }

                if (WrapperCB.SelectedIndex == -1 && WrapperCB.Items.Count > 0)
                {
                    WrapperCB.SelectedIndex = 0;
                }

                m_currentDirectory = new FileInfo(dialog.FileName).DirectoryName;
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Initializes the controls.
        /// </summary>
        private void Initialize()
        {
            DotNetServerCB.Items.Clear();

            List <DotNetOpcServer> servers = DotNetOpcServer.EnumServers();

            foreach (DotNetOpcServer server in servers)
            {
                DotNetServerCB.Items.Add(server);
            }

            DotNetServerCB.SelectedIndex = -1;

            WrapperCB.Items.Clear();

            List <DotNetOpcServerWrapper> wrappers = DotNetOpcServerWrapper.EnumWrappers();

            foreach (DotNetOpcServerWrapper wrapper in wrappers)
            {
                WrapperCB.Items.Add(wrapper);
            }

            ParametersCTRL.Initialize(null);
        }
        /// <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);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Initializes and displays a modal dialog.
        /// </summary>
        public RegisteredDotNetOpcServer ShowDialog(RegisteredDotNetOpcServer registeredServer)
        {
            Initialize();

            m_registeredServer = registeredServer;

            if (registeredServer != null)
            {
                // select the .NET server.
                bool found = false;

                for (int ii = 0; ii < DotNetServerCB.Items.Count; ii++)
                {
                    DotNetOpcServer server = DotNetServerCB.Items[ii] as DotNetOpcServer;

                    if (server.Clsid == registeredServer.ServerClsid)
                    {
                        DotNetServerCB.SelectedIndex = ii;
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    DotNetServerCB.SelectedIndex = DotNetServerCB.Items.Add(new DotNetOpcServer(registeredServer.ServerClsid));
                }

                // select the wrapper process.
                found = false;

                for (int ii = 0; ii < WrapperCB.Items.Count; ii++)
                {
                    DotNetOpcServerWrapper wrapper = WrapperCB.Items[ii] as DotNetOpcServerWrapper;

                    if (wrapper.Clsid == registeredServer.WrapperClsid)
                    {
                        WrapperCB.SelectedIndex = ii;
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    WrapperCB.SelectedIndex = WrapperCB.Items.Add(new DotNetOpcServerWrapper(registeredServer.WrapperClsid));
                }

                // set the remaining parameters.
                ClsidTB.Text       = registeredServer.Clsid.ToString();
                ProgIdTB.Text      = registeredServer.ProgId;
                DescriptionTB.Text = registeredServer.Description;

                ParametersCTRL.Initialize(registeredServer);
            }

            if (DotNetServerCB.SelectedIndex == -1 && DotNetServerCB.Items.Count > 0)
            {
                DotNetServerCB.SelectedIndex = 0;
            }

            if (WrapperCB.SelectedIndex == -1 && WrapperCB.Items.Count > 0)
            {
                WrapperCB.SelectedIndex = 0;
            }

            ShowDialog();

            if (DialogResult != DialogResult.OK)
            {
                return(null);
            }

            return(m_registeredServer);
        }
        /// <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);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Processes the command line arguments.
        /// </summary>
        private static bool ProcessCommandLine()
        {
            string commandLine = Environment.CommandLine;

            List <string> tokens = new List <string>();

            bool          quotedToken = false;
            StringBuilder token       = new StringBuilder();

            for (int ii = 0; ii < commandLine.Length; ii++)
            {
                char ch = commandLine[ii];

                if (quotedToken)
                {
                    if (ch == '"')
                    {
                        if (token.Length > 0)
                        {
                            tokens.Add(token.ToString());
                            token = new StringBuilder();
                        }

                        quotedToken = false;
                        continue;
                    }

                    token.Append(ch);
                }
                else
                {
                    if (token.Length == 0)
                    {
                        if (ch == '"')
                        {
                            quotedToken = true;
                            continue;
                        }
                    }

                    if (Char.IsWhiteSpace(ch))
                    {
                        if (token.Length > 0)
                        {
                            tokens.Add(token.ToString());
                            token = new StringBuilder();
                        }

                        continue;
                    }

                    token.Append(ch);
                }
            }

            if (token.Length > 0)
            {
                tokens.Add(token.ToString());
            }

            // launch gui if no arguments provided.
            if (tokens.Count == 1)
            {
                return(false);
            }

            bool silent = false;

            for (int ii = 1; ii < tokens.Count; ii++)
            {
                if (tokens[ii] == "-?")
                {
                    StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("Opc.ConfigTool.HelpText.txt"));
                    MessageBox.Show(reader.ReadToEnd(), "Opc.ConfigTool");
                    reader.Close();
                    return(true);
                }

                if (tokens[ii] == "-s")
                {
                    silent = true;
                    continue;
                }

                try
                {
                    if (tokens[ii] == "-ra")
                    {
                        if (tokens.Count - ii != 2)
                        {
                            throw new ArgumentException("Incorrect number of parameters specified with the -ra option.");
                        }

                        DotNetOpcServer.RegisterAssembly(tokens[ii + 1]);
                        return(true);
                    }

                    if (tokens[ii] == "-ua")
                    {
                        if (tokens.Count - ii != 2)
                        {
                            throw new ArgumentException("Incorrect number of parameters specified with the -ua option.");
                        }

                        DotNetOpcServer.UnregisterAssembly(tokens[ii + 1]);
                        return(true);
                    }

                    if (tokens[ii] == "-rx")
                    {
                        if (tokens.Count - ii != 2)
                        {
                            throw new ArgumentException("Incorrect number of parameters specified with the -rx option.");
                        }

                        RegisteredDotNetOpcServer.Import(tokens[ii + 1], true);
                        return(true);
                    }

                    if (tokens[ii] == "-ux")
                    {
                        if (tokens.Count - ii != 2)
                        {
                            throw new ArgumentException("Incorrect number of parameters specified with the -ux option.");
                        }

                        RegisteredDotNetOpcServer.Import(tokens[ii + 1], false);
                        return(true);
                    }
                }
                catch (Exception e)
                {
                    if (!silent)
                    {
                        new ExceptionDlg().ShowDialog("Opc.ConfigTool", e);
                        return(true);
                    }
                }
            }

            return(true);
        }