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.
我正在使用TADOConnection的ConnectionObject属性实现包含共享ADO连接的DLL,并将它跨越DLL边界传递到TADOConnection的其他实例。我需要确保COM已初始化,因此我需要调用CoInitialize / CoUninitialize。目前它位于VCL主线程的上下文中,但可能位于另一个线程的其他位置,当然这需要自己的COM实例。我目前没有实现多线程。
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?
在哪里适当地称呼这些; DLL内部(在加载/卸载期间),DLL外(调用进程),还是两者兼而有之?考虑到它只有一个线程,它不应该只是原始进程中DLL之外的一次吗?
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.
我假设原始调用线程应该对此负责,因为COM在线程的上下文中运行。当然在双方调用它们不应该伤害任何一个,但它也会创建多个COM实例。
Long story short... Is it "safe to be safe" in this case? Or is it important to only keep one COM instance?
长话短说......在这种情况下,“安全吗?”或者只保留一个COM实例很重要?
1 个解决方案
#1
6
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.
你不应该在DLL中这样做。使其成为DLL和主机之间的合同的一部分,主机负责初始化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.
不能期望DLL初始化COM,因为主机可能已经这样做了。并使用不同的线程模型。初始化COM后,如果尝试更改线程模式,将来尝试初始化将失败。
So, don't initialize COM in your DLL. Require the host to do so.
因此,不要在DLL中初始化COM。要求主持人这样做。
#1
6
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.
你不应该在DLL中这样做。使其成为DLL和主机之间的合同的一部分,主机负责初始化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.
不能期望DLL初始化COM,因为主机可能已经这样做了。并使用不同的线程模型。初始化COM后,如果尝试更改线程模式,将来尝试初始化将失败。
So, don't initialize COM in your DLL. Require the host to do so.
因此,不要在DLL中初始化COM。要求主持人这样做。