在DLL中使用IXMLDocument需要CoInitialize? [英] Using IXMLDocument inside a DLL needs CoInitialize?

查看:198
本文介绍了在DLL中使用IXMLDocument需要CoInitialize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 IXMLDocument (msxml wrapper)时,有没有理由在我的DLL函数内使用 CoInitialize ? (或其他com对象)



调用应用程序/线程是否负责调用 CoInitialize / CoUninitialize



如果我在我的DLL中使用 ComObj 如果自动调用 CoInitialize 在其初始化部分和 CoUninitialize 在其完成部分?

解决方案

blockquote>

呼叫应用程序/线程是否负责调用CoInitialize / CoUninitialize?


是。作为一般规则,线程的创建者必须负责初始化COM。这意味着您从DLL公开的函数不应该为调用它们的线程初始化COM。



原因是如果您负责在调用DLL的线程中初始化COM,那么对该线程的创建者会产生不合理的约束。如果线程的创建者需要执行需要初始化COM的另一个操作呢?标准的做法是,DLL将COM初始化指定为其要求之一。在您的DLL的文档中,声明调用者必须初始化COM。



更多详细信息:你应该做的事情t做,第2部分:Dll不能在应用程序的线程上调用CoInitialize *。



这就是为什么你不应该在你的DLL中初始化COM主叫的线程。还有更多的原因不要在初始化部分初始化COM。初始化部分在DLL的 DllMain 内执行。在 DllMain 中允许调用的东西很少,COM功能不在允许的操作列表中。有关详细信息,请执行以下操作:您的DllMain中的某些原因不要做任何可怕的部分3


如果我在我的DLL中使用ComObj,它会在其初始化部分自动调用CoInitialize,在其完成部分中调用CoUninitialize ?


ComObj 单位不会这样做。对于DLL,您引用的COM初始化代码被抑制。包括 ComObj 将强制COM在可执行项目中初始化,但不会在库中。


Is there ever a reason to use CoInitialize inside my DLL function when using IXMLDocument (msxml wrapper)? (or Other com object for that matter)

Is the calling application/thread responsible for calling CoInitialize/CoUninitialize?

What if I use ComObj inside my DLL which automatically calls CoInitialize in its initialization section and CoUninitialize in its finalization section?

解决方案

Is the calling application/thread responsible for calling CoInitialize/CoUninitialize?

Yes. As a general rule, the creator of a thread must be responsible for initializing COM. Which means that the functions that you expose from your DLL should not initialize COM for the thread on which they were called.

The reason being that if you take responsibility for initializing COM in the thread that calls the DLL, then that places an unreasonable constraint on the creator of that thread. What if the creator of the thread needs to perform another action that requires COM to be initialized? The standard practise is that the DLL specifies COM initialization as one of its requirements. In the documentation for your DLL, state that the caller must initialize COM.

More details here: Things you shouldn't do, part 2: Dlls can't ever call CoInitialize* on the application’s thread.

That's why you should not initialize COM in your DLL on the caller's thread. There's even more reason not to initialize COM in an initialization section. Initialization sections are executed inside the DLL's DllMain. There are very few things that you are allowed to call inside DllMain, COM functions not being on the list of allowed actions. For more details: Some reasons not to do anything scary in your DllMain, part 3.

What if I use ComObj inside my DLL which automatically calls CoInitialize in its initialization section and CoUninitialize in its finalization section?

The ComObj unit does not do that. For a DLL, the COM initializing code that you refer to is suppressed. Including ComObj will force COM to be initialized in an executable project, but not in a library.

这篇关于在DLL中使用IXMLDocument需要CoInitialize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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