I have in-proc (DLL) COM Server, but I settled in to run as DllSurrogate, for this reason from unmanaged code (Delphi) i have:
function TComWrapper.GetServer: IUnknown;
begin
OleCheck(CoCreateInstance(ServerData^.ClassId, nil, CLSCTX_LOCAL_SERVER, IUnknown, Result));
end;
from C# am using now:
[DllImport("ole32.dll", EntryPoint = "CoCreateInstance", CallingConvention = CallingConvention.StdCall)]
static extern UInt32 CoCreateInstance([In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
IntPtr pUnkOuter, UInt32 dwClsContext, [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid,
[MarshalAs(UnmanagedType.IUnknown)] out object rReturnedComObject);
...
UInt32 dwRes = CoCreateInstance(ClassIdGuid,
IntPtr.Zero,
(uint)(CLSCTX.CLSCTX_LOCAL_SERVER), //if OR with CLSCTX_INPROC_SERVER then INPROC Server will be created, because of DLL COM Server
IUnknownGuid,
out instance);
Above code is unsafe.
Does it exists safe version fro above CoCreateInstance
?
It seems that Activator.CreateInstance
doesn't help me. I have to set explicitly running Context (3rd parameter)
You could also do this:
and create an instance of the COM Object like this:
Without using p/invoke.