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); }