Sunday, June 22, 2008

your bho and your activex control can't be the same object

Frequently I see people writing a BHO, and at some point they realize they need to make a way for a webpage to call some function implemented by the BHO. The best way to do this is to expose an ActiveX control. The most common mistake I see is trying to make the ActiveX control be the same C++ object as the BHO.

This is wrong for several reasons.

It violates good coding practice.

The whole point of object level encapsulation is that each object does one thing. If your BHO is your ActiveX control, now it does two things. It may seem at first that you're really just adding a new method to your BHO, but you have to take into account all that goes along with being an ActiveX control. Implementing IDispatch, loading and dealing with the type library, implementing IObjectSafety, etc.

Your site will be wrong.

Pretty much every IE extension type--toolbands, BHOs, toolbar buttons, ActiveX controls--implements IObjectWithSite. If you make one object be two different types of extension, they will share IObjectWithSite implementations. And they all have a different IE object that is their site.

For example, a toolbar button's site will be an object in IE's toolbar code. A BHO's site is a completely different object in ieframe.dll that knows how to create and interact with a BHO. Explorer bars are sited to an object in ieframe.dll that hosts an explorer bar. And ActiveX controls are sited to an object in mshtml.dll that knows how to host ActiveX controls and implements several DOM interfaces (IHTMLElement, etc) as well.

So if you make your BHO and your ActiveX control the same object, the BHO will be instantiated first and IE will call your IObjectWithSite::SetSite() method with a pointer to one type of object. Then, when your ActiveX control is created later on, IE will call the IObjectWithSite::SetSite() method again, but this time with a different internal IE object's address. Now your BHO is broken. And debugging this sort of thing may take you a long time.

What we Learned Today

Every IE extension you implement must be it's own object, or bad things will start to happen.

No comments: