在 DLL 中调用 CoInitialize/CoUninitialize 的合适位置是什么?
What's the appropriate place to call CoInitialize/CoUninitialize across DLL's?
我正在执行一个包含 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 的 DLL。我目前没有实现多线程。
在哪里调用这些合适的地方;在 DLL 内部(在 loading/unloading 期间),在 DLL 外部(调用进程),还是两者都有?考虑到只有一个线程,在原来的进程中不应该在DLL之外只有一次吗?
我假设原始调用线程应该单独对此负责,因为 COM 在线程的上下文中运行。当然在两侧调用它们应该不会有任何伤害,但是它也会创建多个 COM 实例。
长话短说...在这种情况下是 "safe to be safe" 吗?还是只保留一个 COM 实例很重要?
你不应该在 DLL 中这样做。使宿主负责初始化COM成为DLL与宿主之间契约的一部分。
不能期望 DLL 初始化 COM,因为主机可能已经这样做了。并使用不同的线程模型。一旦 COM 被初始化,如果他们试图改变线程模式,以后的初始化尝试将会失败。
所以,不要在 DLL 中初始化 COM。要求楼主这样做。
我正在执行一个包含 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 的 DLL。我目前没有实现多线程。
在哪里调用这些合适的地方;在 DLL 内部(在 loading/unloading 期间),在 DLL 外部(调用进程),还是两者都有?考虑到只有一个线程,在原来的进程中不应该在DLL之外只有一次吗?
我假设原始调用线程应该单独对此负责,因为 COM 在线程的上下文中运行。当然在两侧调用它们应该不会有任何伤害,但是它也会创建多个 COM 实例。
长话短说...在这种情况下是 "safe to be safe" 吗?还是只保留一个 COM 实例很重要?
你不应该在 DLL 中这样做。使宿主负责初始化COM成为DLL与宿主之间契约的一部分。
不能期望 DLL 初始化 COM,因为主机可能已经这样做了。并使用不同的线程模型。一旦 COM 被初始化,如果他们试图改变线程模式,以后的初始化尝试将会失败。
所以,不要在 DLL 中初始化 COM。要求楼主这样做。