从工作线程调用(编组)单元线程 COM 接口的 ATL 示例
我正在寻找一个代码示例,该示例说明了如何在 ATL COM Server DLL 中进行跨线程 COM 调用。知道一个吗?
更多详细信息:
我的组件是一个 IE 浏览器帮助程序对象,它需要在后台线程上工作。在从 IE 主线程调用的对象方法之一中,IE 为我提供了一个指向 IE DOM 的指针 (IHtmlWindow2
) 并将其存储在本地成员变量中。
现在我想创建一个工作线程,它可以异步调用我刚刚存储的 COM 接口指针上的方法。
如果重要的话,我的调用将在我从 IE 获取接口指针的方法返回后发生,因此当我尝试从工作线程进行调用时,IE(而不是我的代码)将拥有控制权。
我的组件和主机应用程序都是公寓线程的。
有人知道一个很好的 C++ 示例来展示这是如何完成的吗?
我正在专门寻找一个简短、直接的示例(并且不仅仅是只是指向相关文档的指针),因为我阅读了这个问题及其答案,并被我必须做的学习量吓到了执行从头开始。每当有人建议在编写代码之前阅读 Don Box 的书时,您就知道您将度过一个漫长而混乱的夜晚......:-)
I'm looking for a code sample which illustrates making cross-threaded COM calls in an ATL COM Server DLL. Know one?
More details:
My component is an IE Browser Helper Object which needs to do work on a background thread. In one of my object's methods called from IE's main thread, IE gives me a pointer to the IE DOM (IHtmlWindow2
) and I store it in a local member variable.
Now I want to create a worker thread which can asynchronously call methods on that COM interface pointer that I've just stored.
If it matters, my calls will happen after I return from the method where I got the interface pointer from IE, so IE (and not my code) will have control when I try to make the calls from my worker thread.
Both my component and the host application are Apartment threaded.
Anyone know a good C++ sample showing how this is done?
I'm specifically looking for a short, straightforward sample (and not just a pointer to relevant documentation) because I read this question and its answers and got intimidated at the amount of learning I'd have to do to write an implementation from scratch. Any time someone recommends to read a Don Box book before writing code, you know you're in for a long, confusing night... :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在 COM 中是自动的,它负责处理单元要求,并且如果接口指针具有 STA 关联性,则会编组调用。在线程中使用接口指针之前,您必须对其进行封送,ATL 具有 AtlMarshalPtrInProc() 和 AtlUnmarshalPtr() 辅助方法,使之变得更容易。 CoMarshalInterThreadInterfaceInStream() 或 IGlobalInterfaceTable 如果您想自己执行此操作。
That's automatic in COM, it takes care of apartment requirements and will marshal the call if the interface pointer has STA affinity. You have to marshal the interface pointer before using it in the thread, ATL has the AtlMarshalPtrInProc() and AtlUnmarshalPtr() helper methods to make that easier. CoMarshalInterThreadInterfaceInStream() or IGlobalInterfaceTable if you want to do it yourself.