开发者

Calling javascript on webpage from static method in BHO

开发者 https://www.devze.com 2022-12-21 06:45 出处:网络
I\'m writing a Browser Helper Object for Internet Explorer, and I need to call javascript methods on all open browser instances from a \'static\' (or a \'global\') callback method.

I'm writing a Browser Helper Object for Internet Explorer, and I need to call javascript methods on all open browser instances from a 'static' (or a 'global') callback method.

I'm running into issues that I'm sure are related to multi-threading in COM. My questions are:

  1. Every browser would have its own IWebBrowser2 object pointed to by CComPtr<IWebBrowser2>. If I have a static callback method in my BHO class, which thread would that callback arrive on? Would it be any one of the BHO threads that were created by IE?

  2. I tried to cache all the IWebBrowser2 pointers created in every BHO and access them later from the static method. However, I think there are some issues of (un)marshalling involved. Would this work? Any suggestions on a good/diffe开发者_Python百科rent approach to doing this?


The answer to 1 depends on the threading model that you specify for your BHO. If you specify ThreadingModel as Apartment, COM will marshal all calls to your object (within each iexplore.exe process) to the same thread. If you specify ThreadingModel as Free or Both, your object may be accessed on any thread.

The answer to 2 is yes, it would work. It is generally safe to call COM methods from any thread you like, because each object advertises its threading model when it is registered and COM itself is then responsible for handling any marshalling that must occur as a result.

You might find the following links to articles about COM threading models interesting:

  • Understanding and Using COM Threading Models (MSDN)
  • Understanding COM Apartments, Part I (CodeGuru)
  • Understanding COM Apartments, Part II (CodeGuru)


Use the GlobalInterfaceTable to access objects across threads.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号