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