简体   繁体   English

在C ++ dll中使用COM对象

[英]Using COM Object in C++ dll

I am writing a Win32 C++ DLL that uses the COM object(B.dll) which is made in C#. 我正在写一个Win32 C ++ DLL,它使用C#制作的COM对象(B.dll)。 This dll(A.dll) provides CMyComObject class which creates a COM object and access to it. 此dll(A.dll)提供CMyComObject类,该类创建一个COM对象并对其进行访问。 Here is my code. 这是我的代码。

void CMyComObject::CMyComObject() 
{    
  HRESULT result = CoInitialize(NULL);
  ...
  result = CoCreateInstance(CLSID_COMDLL, NULL, CLSCTX_INPROC_SERVER,     IID_COMDLL, reinterpret_cast<void**>(&MyComObject));
}

void CMyComObject::~CMyComObject() 
{
  ..
  CoUninitialize();
  ..
}

And then, here is a client program that loads A.dll and access to the COM object. 然后,这是一个加载A.dll并访问COM对象的客户端程序。 This program creates several threads which load A.dll and create a COM object concurrently. 该程序将创建几个线程,这些线程将加载A.dll并同时创建一个COM对象。

In this case, Is this correct to use CoInitialize() function or Should I use CoInitializeEx() function with COINIT_MULTITHREADED parameter? 在这种情况下,使用CoInitialize()函数正确吗?还是应该将CoInitializeEx()函数与COINIT_MULTITHREADED参数一起使用? Or Is there any mistake I did? 还是我有任何错误? (I registered B.dll by commanding "reg_asm.exe B.dll B.tlb /codebase") (我通过命令“ reg_asm.exe B.dll B.tlb / codebase”注册了B.dll)

Sorry for my poor English. 对不起,我的英语不好。

Thanks. 谢谢。

You are supposed to use CoInitialize[Ex] / CoUninitialize before and after any COM activity on that thread, and your choice between CoInitialize and CoInitializeEx with specific parameters depends on whether you prefer STA or MTA mode for your thread. 您应该在该线程上的任何COM活动之前和之后使用CoInitialize[Ex] / CoUninitialize ,并且在具有特定参数的CoInitializeCoInitializeEx之间进行选择取决于您是为线程选择STA还是MTA模式。

Having said that, your initialization: 话虽如此,您的初始化:

  1. Does not depend on whether the COM object itself creates any threads 不取决于COM对象本身是否创建任何线程
  2. Does not depend on other parts of your application possibly having other COM activity, including similar instantiation of the same COM class 不依赖于应用程序的其他部分可能具有其他COM活动,包括相同COM类的类似实例化
  3. Entirely depends on your COM activity on the thread in question 完全取决于您在有关线程上的COM活动
  4. Does not normally happen in class constructor; 通常不会在类构造函数中发生; it is typical to have COM initialization associated with top level thread code such as before windows message pump or at the very beginning of the thread procedure; 通常将COM初始化与顶级线程代码相关联,例如在Windows消息泵之前或线程过程的开始时进行; putting it into constructor is an easy way to get into collision eg with another initialization (esp. using different apartment model) or too early uninitialization. 将其放入构造函数中是一种容易发生冲突的简便方法,例如与另一个初始化(尤其是使用其他单元模型)或过早的未初始化。

Summing it all once again, your initialization: 再次汇总所有内容,进行初始化:

  • looks good if you are OK with COM single thread apartment model and you don't pass obtained pointer between threads 如果您对COM单线程单元模型没问题,并且没有在线程之间传递获得的指针,则看起来不错
  • you would be better off moving CoInitialize and CoUninitialize calls out of constructor and associate it with thread code 您最好将CoInitializeCoUninitialize调用移出构造函数并将其与线程代码关联
  • be sure to check returned value to detect failures, esp. 确保检查返回值以检测故障,尤其是。 attempt to initialize mismatching apartment on the thread already having COM initialization 尝试在已经进行COM初始化的线程上初始化不匹配的单元
  • the part you missing is that you have to close all your COM activity before CoUninitialize call, including releasing your MyComObject pointer. 您缺少的部分是必须在CoUninitialize调用之前关闭所有COM活动,包括释放MyComObject指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM