Thursday, February 3, 2011

Hints on how to start writing an Internet Explorer Add-on

Most of the info here are for how to write a Browser Helper Object or (BHO). A BHO is a faceless piece of code that is executes when iexplore.exe (Internet Explorer) and explore.exe (Windows Explorer) launch. Yes, believe it or not by default when you add to one, you are adding to the other. I’ll tell you how to avoid that later. You can also write toolbars, etc, but I don’t really cover that here other than some of the links at the bottom of the page do.

A BHO is the simplest plug-in you can write for IE because there is no UI, it is just sitting there allowing you to hook into IE’s events. The two most common are fired when a page has completed its loading (OnDocumentComplete), and also when navigating (OnBeforeNavigate2)  like when a user clicks the link on a page. You may also want to look at NavigateError which is called when there is an error during navigation.

There is also a RegisterBHO method that gets called when you register your BHO which is just a .dll. It is called RegisterBHO, and has a similar method called UnregisterBHO that gets called when you unregister your BHO. When I say register, I am talking about the days of COM. So, you will need to use regasm to register and unregister your BHO. regasm is included with all (I think) the Visual Studio versions. However, you will need to go to the start menu and then go to the Visual Studio 2XXX item and choose something called something like: Visual Studio Tools then Visual Studio Command Prompt (2010). This will bring regasm into the execution path. The two commands you will need are:

regasm /codebase "BHOHelloWorld.dll"

regasm /unregister /codebase "BHOHelloWorld.dll"

You would use these (or similar) to install and uninstall your BHO.

There is some code you will want to put in your RegisterBHO so that IE will see it and load it on startup. Basically, your code will need to get the GUID that you assigned to your BHO class using the Guid attribute, and then add a new SubKey to

HKLM\Software\\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects

If you don’t want the BHO to be used by Windows Explorer and thus only by Internet Explorer, you need to Add a NoExplorer Key at the following path where <GUID> where <GUID> is the GUID for you BHO and also the SubKey your code should have added to the above path in the registry.

HKLM\Software\\Microsoft\Windows\CurrentVersion\Explorer\Browser Helper Objects\<GUID>\NoExplorer

Set that value to the number 1. If you don’t do this before you register your BHO, you will need to re-register it so that the code will be executed when it registers or just add it manually. Regardless, you may find that after a reboot (or sooner) that Windows Explorer has loaded your BHO and won’t let you build (if you registered the .dll in your bin directory that you are building to). In that case, un-register the .dll, and logout and log back into Windows. Now you should be able to build again. You will always need to have IE closed when you are building also.

I would recommend going to here and downloading this project and opening it. Before you run it, change the RegisterBHO to what I have below.

[ComRegisterFunction]
        public static void RegisterBHO(Type type)
        {
            // if the path doesn't exist then create it
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);

            if (registryKey == null)
                registryKey = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);

            // if the Key for this BHO doesn't exist then create it
            // NOTE: The guid comes from the Guid attribute for this class (not the one in the AssemblyInfo.cs)
            string guid = type.GUID.ToString("B");
            RegistryKey ourKey = registryKey.OpenSubKey(guid);

            if (ourKey == null)
                ourKey = registryKey.CreateSubKey(guid);

            // We only use this BHO in iexplore.exe, not explore.exe, so tell it not to load in explore.exe with the special flag (the name should not be changed)
            ourKey.SetValue("NoExplorer", 1);

            // clean up
            registryKey.Close();
            ourKey.Close();
        }

This will prevent Windows Explorer from loading it (assuming that is what you want as well). Register the .dll using regasm.exe and then open IE. There you have it.

Remember, you can also use Visual Studios debugging facilities. You just have to set your breakpoints, build, open IE, attach to the process (VS | Debug menu | Attach to Process…| look for iexplore.exe in list and click the Attach button. Now navigate or refresh in IE. The debugger should hit your breakpoint if you put it in the BeforeNavigate2 or DocumentComplete event.

If you want to find more info on this stuff I suggest searching for DWebBrowserEvents2 or BeforeNavigate2 or DocumentComplete or better yet start by checking out the links below.

Useful Links

How to attach to Browser Helper Object (BHO) with C# in two minutes
This is where I started from since it is in C#, converts to VS 2010 with no errors, and is easy to follow. I built my first BHO based on this.

Creating Custom Explorer Bars, Tool Bands, and Desk Bands
Explains the different type of add-ons

Developing IE Add-ons
Has links to lots of useful articles, sites, etc. Links to IE Protected Mode considerations. Links to how to write Browser Helper Objects (BHO).

Building Browser Helper Objects with Visual Studio 2005
Good article that seems to apply to VS 2010 as well. Good intro.

15 Seconds: Build a Managed BHO and Plug into the Browser
Good tutorial on Browser Helper Objects (BHO).

1 comment:

Unknown said...

My cousin recommended this blog and she was totally right keep up the fantastic work!

Microsoft Internet Explorer Support