Utility functions used by COM applications.
예제 #1
0
        /// <summary>
        /// Gets the available servers.
        /// </summary>
        /// <param name="factory">The factory.</param>
        /// <param name="specification">The specification.</param>
        private void GetAvailableServers(Opc.Ua.Com.ServerFactory factory, Specification specification)
        {
            Uri[] serverUrls = factory.GetAvailableServers(specification);

            for (int ii = 0; ii < serverUrls.Length; ii++)
            {
                ComServerDescription server = factory.ParseUrl(serverUrls[ii]);

                // don't wrap proxies.
                if (ConfigUtils.CLSID_UaComDaProxyServer == server.Clsid)
                {
                    continue;
                }

                if (ConfigUtils.CLSID_UaComAeProxyServer == server.Clsid)
                {
                    continue;
                }

                if (ConfigUtils.CLSID_UaComHdaProxyServer == server.Clsid)
                {
                    continue;
                }

                // don't wrap UA psuedo-servers.
                List <Guid> catids = ConfigUtils.GetImplementedCategories(server.Clsid);

                bool suppress = false;

                for (int jj = 0; jj < catids.Count; jj++)
                {
                    if (catids[jj] == ConfigUtils.CATID_PseudoComServers)
                    {
                        suppress = true;
                        break;
                    }
                }

                if (suppress)
                {
                    continue;
                }

                // assume regular COM server.
                ListViewItem item = new ListViewItem(server.ProgId);
                item.SubItems.Add(server.Description);
                item.SubItems.Add(specification.ToString());
                item.Tag = new Item(specification, server);

                ServersLV.Items.Add(item);
            }
        }
예제 #2
0
        private void AddTN_Click(object sender, EventArgs e)
        {
            try
            {
                int port = new NewPortDlg().ShowDialog(0);

                if (port > 0)
                {
                    ConfigUtils.SetFirewallAccess(m_application.ExecutablePath, port);
                    UpdatePorts();
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception);
            }
        }
예제 #3
0
        /// <summary>
        /// Displays the dialog.
        /// </summary>
        public bool ShowDialog(ManagedApplication application)
        {
            m_application = application;

            if (m_application != null)
            {
                ApplicationAccessGrantedCK.Checked = ConfigUtils.CheckFirewallAccess(m_application.ExecutablePath, null);
            }

            UpdatePorts();

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

            return(true);
        }
예제 #4
0
        private void UpdatePorts()
        {
            PortsLV.Items.Clear();

            if (m_application != null)
            {
                int[] ports = ConfigUtils.GetFirewallAccess(m_application.ExecutablePath);

                if (ports != null)
                {
                    for (int ii = 0; ii < ports.Length; ii++)
                    {
                        ListViewItem item = new ListViewItem(ports[ii].ToString());
                        item.Tag = ports[ii];
                        PortsLV.Items.Add(item);
                    }
                }
            }

            PortsChanged();
        }
예제 #5
0
        void Port_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CheckBox box = (CheckBox)sender;

                if (box.Checked)
                {
                    ConfigUtils.SetFirewallAccess(m_application.ExecutablePath, (int)box.Tag);
                }
                else
                {
                    ConfigUtils.RemoveFirewallAccess((int)box.Tag);
                }

                UpdatePorts();
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception);
            }
        }
예제 #6
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // check if running in command line mode.
            string[] args = Environment.GetCommandLineArgs();

            try
            {
                if (args.Length > 1)
                {
                    if (ConfigUtils.ProcessCommandLine())
                    {
                        return;
                    }
                }

                ApplicationConfiguration configuration = GuiUtils.DoStartupChecks(
                    "Opc.Ua.ConfigurationTool",
                    ApplicationType.Client,
                    "Opc.Ua.ConfigurationTool.Config.xml",
                    false);

                if (configuration != null)
                {
                    Application.Run(new MainForm(configuration));
                }
            }
            catch (Exception e)
            {
                GuiUtils.HandleException(Utils.Format(
                                             "UA Certificate Tool: {0} {1}",
                                             (args.Length > 1)?args[1]:null,
                                             (args.Length > 2)?args[2]:null),
                                         null,
                                         e);
            }
        }
예제 #7
0
        static void Main(string[] args)
        {
            try
            {
                // check the arguments
                if (args.Length < 2)
                {
                    Console.WriteLine("Not enough command line arguments provided.");
                    return;
                }

                // process command line.
                ConfigUtils.ProcessCommandLine(args);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected error: {0}", e.Message);
            }
            finally
            {
                // Console.ReadLine();
            }
        }
예제 #8
0
        /// <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);
        }
예제 #9
0
 /// <summary>
 /// Deletes the pseudoserver from the registry.
 /// </summary>
 public static void Delete(Guid clsid)
 {
     ConfigUtils.UnregisterComServer(clsid);
     DeleteConfiguredEndpointFile(clsid);
 }
예제 #10
0
        /// <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);
            }
        }