예제 #1
0
        /// <summary>
        /// Connects to the specified COM server server.
        /// </summary>
        public object CreateServer(Uri uri, UserIdentity identity)
        {
            // parse path to find prog id and clsid.
            string progID = uri.LocalPath;
            string clsid  = null;

            while (progID.StartsWith("/"))
            {
                progID = progID.Substring(1);
            }

            int index = progID.IndexOf('/');

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

            // look up prog id if clsid not specified in the uri.
            Guid guid = Guid.Empty;

            if (String.IsNullOrEmpty(clsid))
            {
                // connect to enumerator.
                Connect(uri.Host, identity);

                // use OpcEnum to lookup the prog id.
                guid = CLSIDFromProgID(progID);

                // check if prog id is actually a clsid string.
                if (guid == Guid.Empty)
                {
                    clsid = progID;
                }
            }

            // convert CLSID to a GUID.
            if (guid == Guid.Empty)
            {
                try
                {
                    guid = new Guid(clsid);
                }
                catch (Exception e)
                {
                    throw ServiceResultException.Create(StatusCodes.BadCommunicationError, e, "COM server URI does not contain a valid CLSID or ProgID.");
                }
            }

            // use normal activation.
            return(ComUtils.CreateInstance(guid, uri.Host, identity));

            #if COM_IMPERSONATION_SUPPORT
            // set the current thread token.
            IPrincipal       existingPrincipal = Thread.CurrentPrincipal;
            WindowsPrincipal principal         = ComUtils.GetPrincipalFromUserIdentity(identity);

            try
            {
                if (principal != null)
                {
                    Thread.CurrentPrincipal = principal;
                }

                // activate with a license key if one provided.
                if (identity != null && !String.IsNullOrEmpty(identity.LicenseKey))
                {
                    return(ComUtils.CreateInstanceWithLicenseKey(guid, uri.Host, identity, identity.LicenseKey));
                }

                // use normal activation.
                return(ComUtils.CreateInstance(guid, uri.Host, identity));
            }
            finally
            {
                Thread.CurrentPrincipal = existingPrincipal;
            }
            #endif
        }