/// <summary> /// Creates an instance of a COM server. /// </summary> public static object CreateServer(Guid clsid) { COSERVERINFO coserverInfo = new COSERVERINFO(); coserverInfo.pwszName = null; coserverInfo.pAuthInfo = IntPtr.Zero; coserverInfo.dwReserved1 = 0; coserverInfo.dwReserved2 = 0; GCHandle hIID = GCHandle.Alloc(IID_IUnknown, GCHandleType.Pinned); MULTI_QI[] results = new MULTI_QI[1]; results[0].iid = hIID.AddrOfPinnedObject(); results[0].pItf = null; results[0].hr = 0; try { // create an instance. CoCreateInstanceEx( ref clsid, null, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, ref coserverInfo, 1, results); } finally { hIID.Free(); } if (results[0].hr != 0) { throw new ExternalException("CoCreateInstanceEx: 0x{0:X8}" + results[0].hr); } return results[0].pItf; }
/// <summary> /// Allocates a COSERVERINFO structure. /// </summary> public COSERVERINFO Allocate(string hostName, UserIdentity identity) { // initialize server info structure. COSERVERINFO serverInfo = new COSERVERINFO(); serverInfo.pwszName = hostName; serverInfo.pAuthInfo = IntPtr.Zero; serverInfo.dwReserved1 = 0; serverInfo.dwReserved2 = 0; // no authentication for default identity if (UserIdentity.IsDefault(identity)) { return serverInfo; } m_hUserName = GCHandle.Alloc(identity.Username, GCHandleType.Pinned); m_hPassword = GCHandle.Alloc(identity.Password, GCHandleType.Pinned); m_hDomain = GCHandle.Alloc(identity.Domain, GCHandleType.Pinned); m_hIdentity = new GCHandle(); // create identity structure. COAUTHIDENTITY authIdentity = new COAUTHIDENTITY(); authIdentity.User = m_hUserName.AddrOfPinnedObject(); authIdentity.UserLength = (uint)((identity.Username != null)?identity.Username.Length:0); authIdentity.Password = m_hPassword.AddrOfPinnedObject(); authIdentity.PasswordLength = (uint)((identity.Password != null)?identity.Password.Length:0); authIdentity.Domain = m_hDomain.AddrOfPinnedObject(); authIdentity.DomainLength = (uint)((identity.Domain != null)?identity.Domain.Length:0); authIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; m_hIdentity = GCHandle.Alloc(authIdentity, GCHandleType.Pinned); // create authorization info structure. COAUTHINFO authInfo = new COAUTHINFO(); authInfo.dwAuthnSvc = RPC_C_AUTHN_WINNT; authInfo.dwAuthzSvc = RPC_C_AUTHZ_NONE; authInfo.pwszServerPrincName = IntPtr.Zero; authInfo.dwAuthnLevel = RPC_C_AUTHN_LEVEL_CONNECT; authInfo.dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE; authInfo.pAuthIdentityData = m_hIdentity.AddrOfPinnedObject(); authInfo.dwCapabilities = EOAC_NONE; // EOAC_DYNAMIC_CLOAKING; m_hAuthInfo = GCHandle.Alloc(authInfo, GCHandleType.Pinned); // update server info structure. serverInfo.pAuthInfo = m_hAuthInfo.AddrOfPinnedObject(); return serverInfo; }
public IntPtr CreateInstance(CLSCTX dwContext, string server) { IntPtr pInterface = IntPtr.Zero; if (dwContext == CLSCTX.ALL) { if (DefaultServerType == COMServerType.InProcServer32) { dwContext = CLSCTX.INPROC_SERVER; } else if (DefaultServerType == COMServerType.LocalServer32) { dwContext = CLSCTX.LOCAL_SERVER; } else if (DefaultServerType == COMServerType.InProcHandler32) { dwContext = CLSCTX.INPROC_HANDLER; } else { dwContext = CLSCTX.SERVER; } } Guid iid = COMInterfaceEntry.CreateKnownInterface(COMInterfaceEntry.KnownInterfaces.IUnknown).Iid; Guid clsid = Clsid; int hr = 0; if (server != null) { MULTI_QI[] qis = new MULTI_QI[1]; qis[0] = new MULTI_QI(iid); COSERVERINFO server_info = new COSERVERINFO(server); try { hr = COMUtilities.CoCreateInstanceEx(ref clsid, IntPtr.Zero, dwContext, server_info, 1, qis); if (hr == 0) { hr = qis[0].HResult(); if (hr == 0) { pInterface = qis[0].GetObjectPointer(); } } } finally { ((IDisposable)qis[0]).Dispose(); } } else { hr = COMUtilities.CoCreateInstance(ref clsid, IntPtr.Zero, dwContext, ref iid, out pInterface); } if (hr != 0) { Marshal.ThrowExceptionForHR(hr); } return(pInterface); }
static extern void CoGetInstanceFromIStorage(COSERVERINFO pServerInfo, ref Guid pclsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, CLSCTX dwClsCtx, IStorage pstg, uint cmq, [In, Out] MULTI_QI[] rgmqResults);
static extern void CoGetInstanceFromFile( COSERVERINFO pServerInfo, ref Guid pClsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, CLSCTX dwClsCtx, STGM grfMode, string pwszName, uint cmq, [In, Out] MULTI_QI[] rgmqResults);
static extern void CoCreateInstanceEx( [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter, CLSCTX dwClsCtx, COSERVERINFO pServerInfo, uint cmq, [In, Out] MULTI_QI[] pResults);
internal static extern int CoCreateInstanceEx(ref Guid clsid, IntPtr pUnkOuter, int dwClsContext, [In, Out] COSERVERINFO srv, int num, [In, Out] MULTI_QI[] amqi);