コード例 #1
0
        public static object CreateLocalServer(Guid clsid)
        {
            Utils.COSERVERINFO coserverinfo = default(Utils.COSERVERINFO);
            coserverinfo.pwszName    = null;
            coserverinfo.pAuthInfo   = IntPtr.Zero;
            coserverinfo.dwReserved1 = 0U;
            coserverinfo.dwReserved2 = 0U;
            GCHandle gchandle = GCHandle.Alloc(Utils.IID_IUnknown, GCHandleType.Pinned);

            Utils.MULTI_QI[] array = new Utils.MULTI_QI[1];
            array[0].iid  = gchandle.AddrOfPinnedObject();
            array[0].pItf = null;
            array[0].hr   = 0U;
            try
            {
                Utils.CoCreateInstanceEx(ref clsid, null, 5U, ref coserverinfo, 1U, array);
            }
            finally
            {
                gchandle.Free();
            }
            if (array[0].hr != 0U)
            {
                throw new ExternalException("CoCreateInstanceEx: 0x{0:X8}" + array[0].hr);
            }
            return(array[0].pItf);
        }
コード例 #2
0
        public static object CreateInstance(Guid clsid, string hostName, string username, string password, string domain)
        {
            Utils.ServerInfo   serverInfo   = new Utils.ServerInfo();
            Utils.COSERVERINFO coserverinfo = serverInfo.Allocate(hostName, username, password, domain);
            GCHandle           gchandle     = GCHandle.Alloc(Utils.IID_IUnknown, GCHandleType.Pinned);

            Utils.MULTI_QI[] array = new Utils.MULTI_QI[1];
            array[0].iid  = gchandle.AddrOfPinnedObject();
            array[0].pItf = null;
            array[0].hr   = 0U;
            try
            {
                uint dwClsCtx = 5U;
                if (!string.IsNullOrEmpty(hostName) && hostName != "localhost")
                {
                    dwClsCtx = 20U;
                }
                Utils.CoCreateInstanceEx(ref clsid, null, dwClsCtx, ref coserverinfo, 1U, array);
            }
            finally
            {
                if (gchandle.IsAllocated)
                {
                    gchandle.Free();
                }
                serverInfo.Deallocate();
            }
            if (array[0].hr != 0U)
            {
                throw Utils.CreateComException(-2147467259, "Could not create COM server '{0}' on host '{1}'. Reason: {2}.", new object[]
                {
                    clsid,
                    hostName,
                    Utils.GetSystemMessage((int)array[0].hr, 2048)
                });
            }
            return(array[0].pItf);
        }