Browser Helper Object (BHO) Sample

ChiefRedBull
12-22-2002, 07:46 AM
A simple (when I say simple, I MEAN simple.. :)) example of how to
attach a class object to each and every instance of Internet
Explorer, and Windows Explorer.

Here's a little background info on how BHOs work:

Each time a new instance of Explorer (whether it's Internet
Explorer or Windows Explorer) is loaded, it searches through a
specific part of the registry for BHOs. These are then loaded and
instantiated. Now, here's where it gets a bit compocated - since
Internet Explorer must load these objects, it must be able to
have a variable to store a reference at some point. In VB, if you
want a string, then you declare a String. With objects, you declare
the class type -

Dim oMyClass As Class1

What you're really doing is declaring a reference to the default
interface that class supports. A class' interface is it's properties,
methods and events, and their parameters. It's a description of
that class. Since all the BHOs may well have totally different
functions to perform, they may also have totally different
interfaces, and this causes Internet Explorer a problem. To over
come this problem, each BHO MUST implement a certain interface:
IObjectWithSite, which looks like this:

Private Sub IObjectWithSite_GetSite(ByVal priid As VBShellLib.REFIID, ppvObj As VBShellLib.VOID)
'
End Sub

Private Sub IObjectWithSite_SetSite(ByVal pSite As VBShellLib.IUnknownVB)
'
End Sub

These interfaces are not built into VB, and we can't just declare
them as an empty class module, due to the inner workings of
VB. As such, we're reliant on Type Libraries - files that contain
interface definitions (amongst other things). I've included
VBShell.tlb, available from Edanmo's VB site. (http://www.domaindlx.com/e_morcillo/)

So.. how do we attach to Internet Explorer? Well, when the
BHO is first loaded, SetSite will be called, and a reference to the
browser passed via pSite. All we have to do is store this
pointer, convert it into an InternetExplorer object (which uses
the IWebBrowser2 interface) and we're done.

There's a couple of other things on BHOs that you should know,
but for further reading I suggest these pages:

http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/ext/extensions.asp
http://msdn.microsoft.com/library/en-us/dnwebgen/html/bho.asp


To make the extension included work, simply run the Register.reg
file, then open a new InternetExplorer. You should see a MsgBox
containing the URL of each link BEFORE Internet Explorer begins
navigating to it. To remove the BHO, just run Un-Register.reg.
Have a look inside the REG files to see which bits of the registry
Internet Explorer searches in.

Oh, one other point. The thing about BHOs (and all compiled
ActiveX DLLs for that matter) is that they all have to be uniquely
identified. This requires what's known as a CLSID (Class ID).
This is used when registering the BHO to Internet Explorer, and
if it changes, then your BHO won't load properly. As such, you
MUST have the Binary Compatibility option set on the BHO DLL.
Also, you CANNOT break the class' interface if you want to have
the same CLSID - once the interface changes, so must the CLSID.
I don't want to go into too much detail, but I suggest that you
read Thinker's magnificent tutorial on COM and interfaces, to be
found here:

http://www.visualbasicforum.com/showthread.php?threadid=26521


Enjoy :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum