C# .NET中的CoCreateInstance精确匹配?
我在proc(DLL)COM服务器中安装了,但我选择作为DllSurrogate运行,基于此原因,在非托管代码(Delphi)中,我有: 从C#am开始,现在使用:C# .NET中的CoCreateInstance精确匹配?,c#,.net,windows,com,interop,C#,.net,Windows,Com,Interop,我在proc(DLL)COM服务器中安装了,但我选择作为DllSurrogate运行,基于此原因,在非托管代码(Delphi)中,我有: 从C#am开始,现在使用: [DllImport("ole32.dll", EntryPoint = "CoCreateInstance", CallingConvention = CallingConvention.StdCall)] static extern UInt32 CoCreateInstance([In, MarshalAs(Unmanaged
[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);
以上代码是不安全的。
它是否存在上面的安全版本CoCreateInstance
?
似乎
Activator.CreateInstance
对我没有帮助。我必须明确设置运行上下文(第3个参数)您也可以这样做:
[ComImport]
[Guid(YourGuidGoesHere)]
private class MyClass
{
}
并创建COM对象的实例,如下所示:
IYourInterface myClass = (IYourInterface)(new MyClass());
不使用p/invoke。正确的pinvoke声明没有什么不安全的。请注意,您在任何地方都不必使用不安全关键字。@HansPassant,因此在
unsafe{…}
中包装CoCreateInstance
是没有用的?您能不能在代码中将其作为COM组件运行?@ALZ yes<代码>不安全块基本上用于不安全的指针操作,您不需要执行任何操作。类后COM关键字是什么?
[ComImport]
[Guid(YourGuidGoesHere)]
private class MyClass
{
}
IYourInterface myClass = (IYourInterface)(new MyClass());