在DLL中调用CoInitialize / CoUninitialize的适当位置是什么? [英] What's the appropriate place to call CoInitialize/CoUninitialize across DLL's?

查看:523
本文介绍了在DLL中调用CoInitialize / CoUninitialize的适当位置是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用包含共享ADO连接的DLL ConnectionObject 属性 TADOConnection ,并将其跨越DLL边界传递到其他实例 TADOConnection 。我需要确保COM被初始化,所以我需要调用 CoInitialize / CoUninitialize 。目前它是在VCL主线程的上下文中,但可能在另一个线程的其他位置,当然需要自己的COM实例。我目前没有实施多线程。

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.

在哪里可以调用这些;在DLL内(在加载/卸载期间),在DLL(调用进程)之外,还是两者?考虑到这只是一个线程,在原始进程中不应该只是DLL之外的一次?

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?

我假设原来的调用线程应该是全权负责这是因为COM在线程的上下文中运行。当然,双方都不应该伤害任何一个COM,但是它也会创建多个COM实例。

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实例?

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

推荐答案

您不应该在DLL中执行此操作。使其成为主机负责初始化COM的DLL和主机之间的合同的一部分。

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初始化COM。并使用不同的线程模型。一旦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。要求主机这样做。

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

这篇关于在DLL中调用CoInitialize / CoUninitialize的适当位置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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