Пример #1
0
        //======================================================================
        // IOpcFactory

        /// <summary>
        /// Creates a new instance of the server.
        /// </summary>
        public override Technosoftware.DaAeHdaClient.IOpcServer CreateInstance(Technosoftware.DaAeHdaClient.OpcUrl url, Technosoftware.DaAeHdaClient.OpcConnectData connectData)
        {
            object comServer = Factory.Connect(url, connectData);

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

            Technosoftware.DaAeHdaClient.Com.Server server = null;
            Type interfaceType = null;

            try
            {
                if (String.IsNullOrEmpty(url.Scheme))
                {
                    throw new NotSupportedException(String.Format("The URL scheme '{0}' is not supported.", url.Scheme));
                }

                // DA
                else if (url.Scheme == Technosoftware.DaAeHdaClient.OpcUrlScheme.DA)
                {
                    // Verify that it is a DA server.
                    if (!typeof(OpcRcw.Da.IOPCServer).IsInstanceOfType(comServer))
                    {
                        interfaceType = typeof(OpcRcw.Da.IOPCServer);
                        throw new NotSupportedException();
                    }

                    // DA 3.00
                    if (!ForceDa20Usage && typeof(OpcRcw.Da.IOPCBrowse).IsInstanceOfType(comServer) && typeof(OpcRcw.Da.IOPCItemIO).IsInstanceOfType(comServer))
                    {
                        server = new Technosoftware.DaAeHdaClient.Com.Da.Server(url, comServer);
                    }

                    // DA 2.XX
                    else if (typeof(OpcRcw.Da.IOPCItemProperties).IsInstanceOfType(comServer))
                    {
                        server = new Technosoftware.DaAeHdaClient.Com.Da20.Server(url, comServer);
                    }

                    else
                    {
                        interfaceType = typeof(OpcRcw.Da.IOPCItemProperties);
                        throw new NotSupportedException();
                    }
                }

                // AE
                else if (url.Scheme == Technosoftware.DaAeHdaClient.OpcUrlScheme.AE)
                {
                    // Verify that it is a AE server.
                    if (!typeof(OpcRcw.Ae.IOPCEventServer).IsInstanceOfType(comServer))
                    {
                        interfaceType = typeof(OpcRcw.Ae.IOPCEventServer);
                        throw new NotSupportedException();
                    }

                    server = new Technosoftware.DaAeHdaClient.Com.Ae.Server(url, comServer);
                }

                // HDA
                else if (url.Scheme == Technosoftware.DaAeHdaClient.OpcUrlScheme.HDA)
                {
                    // Verify that it is a HDA server.
                    if (!typeof(OpcRcw.Hda.IOPCHDA_Server).IsInstanceOfType(comServer))
                    {
                        interfaceType = typeof(OpcRcw.Hda.IOPCHDA_Server);
                        throw new NotSupportedException();
                    }

                    server = new Technosoftware.DaAeHdaClient.Com.Hda.Server(url, comServer);
                }

                // All other specifications not supported yet.
                else
                {
                    throw new NotSupportedException(String.Format("The URL scheme '{0}' is not supported.", url.Scheme));
                }
            }
            catch (NotSupportedException e)
            {
                Technosoftware.DaAeHdaClient.Utilities.Interop.ReleaseServer(server);
                server = null;

                if (interfaceType != null)
                {
                    StringBuilder message = new StringBuilder();

                    message.AppendFormat("The COM server does not support the interface ");
                    message.AppendFormat("'{0}'.", interfaceType.FullName);
                    message.Append("\r\n\r\nThis problem could be caused by:\r\n");
                    message.Append("- incorrectly installed proxy/stubs.\r\n");
                    message.Append("- problems with the DCOM security settings.\r\n");
                    message.Append("- a personal firewall (sometimes activated by default).\r\n");

                    throw new NotSupportedException(message.ToString());
                }

                throw e;
            }
            catch (Exception e)
            {
                Technosoftware.DaAeHdaClient.Utilities.Interop.ReleaseServer(server);
                server = null;

                throw e;
            }

            // initialize the wrapper object.
            if (server != null)
            {
                server.Initialize(url, connectData);
            }

            return(server);
        }
Пример #2
0
        /// <summary>
        /// Connects to the specified COM server.
        /// </summary>
        public static object Connect(Technosoftware.DaAeHdaClient.OpcUrl url, Technosoftware.DaAeHdaClient.OpcConnectData connectData)
        {
            // parse path to find prog id and clsid.
            string progID = url.Path;
            string clsid  = null;

            int index = url.Path.IndexOf('/');

            if (index >= 0)
            {
                progID = url.Path.Substring(0, index);
                clsid  = url.Path.Substring(index + 1);
            }

            // look up prog id if clsid not specified in the url.
            Guid guid;

            if (clsid == null)
            {
                // use OpcEnum to lookup the prog id.
                guid = new ServerEnumerator().CLSIDFromProgID(progID, url.HostName, connectData);

                // check if prog id is actually a clsid string.
                if (guid == Guid.Empty)
                {
                    try
                    {
                        guid = new Guid(progID);
                    }
                    catch
                    {
                        throw new OpcResultException(new OpcResult((int)OpcResult.CO_E_CLASSSTRING.Code, OpcResult.FuncCallType.SysFuncCall, null), String.Format("Could not connect to server {0}", progID));
                    }
                }
            }

            // convert clsid string to a guid.
            else
            {
                try
                {
                    guid = new Guid(clsid);
                }
                catch
                {
                    throw new OpcResultException(new OpcResult((int)OpcResult.CO_E_CLASSSTRING.Code, OpcResult.FuncCallType.SysFuncCall, null), String.Format("Could not connect to server {0}", progID));
                }
            }

            // get the credentials.
            OpcUserIdentity credentials = (connectData != null) ? connectData.UserIdentity : null;

            // instantiate the server using CoCreateInstanceEx.
            if (connectData == null || connectData.LicenseKey == null)
            {
                try
                {
                    return(Technosoftware.DaAeHdaClient.Utilities.Interop.CreateInstance(guid, url.HostName, credentials));
                }
                catch (Exception e)
                {
                    throw new OpcResultException(OpcResult.CO_E_CLASSSTRING, e.Message, e);
                }
            }

            // instantiate the server using IClassFactory2.
            else
            {
                try
                {
                    return(null);
                    //return Technosoftware.DaAeHdaClient.Utilities.Interop.CreateInstanceWithLicenseKey(guid, url.HostName, credentials, connectData.LicenseKey);
                }
                catch (Exception e)
                {
                    throw new OpcResultException(OpcResult.CO_E_CLASSSTRING, e.Message, e);
                }
            }
        }