What's the appropriate place to call CoInitialize/CoUninitialize across DLL's?

亡梦爱人 提交于 2019-12-11 09:53:31

问题


I'm implementing a DLL containing a shared ADO Connection by using the ConnectionObject property of TADOConnection and passing it across DLL boundaries into other instances of TADOConnection. I need to make sure COM is initialized, so I need to call CoInitialize / CoUninitialize. Currently it's in the context of a VCL main thread, but could be elsewhere in another thread, which of course requires its own instance of COM. I'm not currently implementing multi-threading.

Where is the appropriate place to call these; inside the DLL (during loading/unloading), outside the DLL (calling process), or both? Considering it's only one thread, shouldn't it be only one time outside the DLL in the original process?

I'm assuming the original calling thread should be solely responsible for this, since COM runs in the context of a thread. Of course calling them on both sides shouldn't hurt any, however it would also create multiple COM instances.

Long story short... Is it "safe to be safe" in this case? Or is it important to only keep one COM instance?


回答1:


You should not be doing this in the DLL. Make it part of the contract between the DLL and the host that the host is responsible for initializing COM.

The DLL cannot be expected to initialize COM because the host may already have done so. And use a different threading model. Once COM has been initialized then future attempts to initialize will fail if they attempt to change the threading mode.

So, don't initialize COM in your DLL. Require the host to do so.



来源:https://stackoverflow.com/questions/30020493/whats-the-appropriate-place-to-call-coinitialize-couninitialize-across-dlls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!