CoCreateInstance的精确匹配的.NET? [英] CoCreateInstance exact match in .NET?

查看:220
本文介绍了CoCreateInstance的精确匹配的.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在进程内(DLL)COM服务器,但我坐下来为DllSurrogate运行,为此从非托管code(德尔福)我有:

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;

从C#现在使用:

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

以上code是不安全的。 它是否存在安全版本来回回上述的CoCreateInstance ? 看来, Activator.CreateInstance 并不能帮助我。我要明确设置运行的上下文(第三个参数)

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)

推荐答案

您也可以做到这一点:

[ComImport]
[Guid(YourGuidGoesHere)]
private class MyClass
{
}

和创建这样的COM对象的实例:

and create an instance of the COM Object like this:

  IYourInterface myClass = (IYourInterface)(new MyClass());

在不使用P / Invoke。

Without using p/invoke.

这篇关于CoCreateInstance的精确匹配的.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆