IE DOM Event Handlers in C++

Newspaper
Getting C++ code called when there’s document event is pretty complicated. You have to create a COM class that implements the IDispatch interface, package that into a VARIANT object, and then call the element or document interface to attach it.

I’ve put together a small sample project showing how to do it, a simple BHO that attaches a callback to the document onclick event. It contains a generic C++ helper class that implements IDispatch, and calls back to a user-defined function when the event occurs. You can reuse this class for handling any DOM events. Download it all here.

This is the same technique I’m using in the IE port of PeteSearch, and it’s working well. You do have to be careful of threading issues in your callback though, since you don’t know which thread will run it! My thanks go to Ian Hart from AppxWeb, he pointed me in the right direction with this MSDN forum post.

More posts on porting Firefox add-ons to IE

Leave a comment