Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET中的CoCreateInstance精确匹配?_C#_.net_Windows_Com_Interop - Fatal编程技术网

C# .NET中的CoCreateInstance精确匹配?

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

我在proc(DLL)COM服务器中安装了,但我选择作为DllSurrogate运行,基于此原因,在非托管代码(Delphi)中,我有:

从C#am开始,现在使用:

[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());