How to attach to Browser Helper Object (BHO) with C# in two minutes (2009-10-01)
Introduction
Microsoft provided Browser Helper Object (BHO) to let developers "drive" Internet Explorer. The first BHO was introduced in 1997 with IE 4.0. I have been writing programs on BHO for months. It could be quite depressing at the very first beginning to learn all those things. Hereby, I am writing this article to help beginners like me get familiar with BHO as soon as possible.
Background
My personal interest is actually C++. C++ programs can be a lot less memory-consuming than C# programs. But C# does provide better service on BHO comparing to C++. My first BHO program was written in C++. It took me quite a while to figure out how to handle BHO under V C++. But C# only takes me few minutes. Meanwhile, C# has lots of pleasant designs such as foreach and type conversion.
Process
To set up a BHO Hello World Project, Lets first start a C# Class Library, as BHO is written in .dll attached to IE. You dont need a Visual Studio 2005, C# express is totally enough.
After we have this empty project, let's add one folder named BHO and an empty .csfile into the folder.
GetSite: Gets the last site set with IObjectWithSite::SetSite. If there is no known site, the object returns a failure code.
SetSite: Provides the site's IUnknown pointer to the object.
publicinterface IObjectWithSite
{
[PreserveSig]
int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
[PreserveSig]
int GetSite(ref Guid guid, out IntPtr ppvSite);
}
Don't forget
using System.Runtime.InteropServices;
Add another .cs file named BHO.cs
Add a class called BHO in the newly added file(BHO.cs) . The class contains the interface IObjectWithSite
class BHO:IObjectWithSite
To use BHO we need to have two references, SHDocVw.dll and MSHTML.dll.You can find them at Windows\System32 folder
SHDocVw is Microsoft Shell Doc Object and Control Library
MSHTML is: All interfaces for accessing the Dynamic HTML (DHTML) Object Model are based on IDispatch and are the basis of access to the object model that is also used by scripts. http://msdn2.microsoft.com/en-us/library/bb498651.aspx
using SHDocVw;
using mshtml;
using System.Runtime.InteropServices;
namespace BHO_HelloWorld
{
class BHO:IObjectWithSite
{}
}
have "using SHDocVw" is not enough, you need to add references to the project.
Add SHDocVw
Later we are going to use MessageBox, we also need to add Windows Form reference
Now under BHO.cs:
we add two variables into the class, WebBrowser and HTMLDocument. Just like their name, you could easily figure out what do they do.
Besides the two methods we defined in the IObjectWithSite interface, we also need to add OnDocumentComplete. You can name the function anything you want as long as the parameters are the same as what's defined in CDHtmlDialog class. Later you need to carfully attach to an EventHandler. For the consistency of code, we name it OnDocumentComplete. OnDocumentComplete is a function of CDHtmlDialog Class http://msdn2.microsoft.com/en-us/library/8bed8k60(VS.80).aspx . It will be triggered if the HTMLDocument downloading is complete, in other words, when your page is loaded. You can also use Navigate() or OnBeforeNavigate(). Please refer to http://msdn2.microsoft.com/en-us/library/8k5z3ekh(VS.80).aspx to find out what you need exactly.
you need to point out the GUID of IE for thei program, so it can attach to IE.
[
ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
]
publicinterface IObjectWithSite
{
[PreserveSig]
int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
[PreserveSig]
int GetSite(ref Guid guid, out IntPtr ppvSite);
}
Also, you need to assian a GUID for your own program under BHO.cs. You can use System.Guid.NewGuid() method to get one, which is really neat comparing to C++.
You cannot just leave SetSite and GetSite blank. fill them in. This step is to tell IE that the DocumentCompletent Event is attached to OnDocumentComplete in our program.
publicint SetSite(object site)
{
if (site != null)
{
webBrowser = (WebBrowser)site;
webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
}
else
{
webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
webBrowser = null;
}
return0;
}
publicint GetSite(ref Guid guid, out IntPtr ppvSite)
{
IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
Marshal.Release(punk);
return hr;
}
Add one more reference
using Microsoft.Win32;
Under BHO.cs we need to write two functions for register/unregister of this DLL.
Now compile, under your release folder, you will find the .dll of your own project.
Then, use regasm /codebase "BHO HelloWorld.dll" to register our dll. We got a problem here. The REGASM told me it's not registerd. WHY?
Because we didn't set the BHO class as public. That's why.
publicclass BHO:IObjectWiteSite
now, do it again. It's successful.
open your registry. Find out Browser Helper Object under LOCAL_MACHINE->SOFTWARE->MICROSOFT->WINDOWS->EXPLORER
So, now program has been officially attached to your BHO. We need to fillin the OnDocumentComplete function. It's really neat to use C#'s foreach loop rather than for loop in C++. So you won't need to care about the indexer. We will also see the type conversion is quite easy. This is an example on we want to find out the NAME attributes of an IHTMLInputElement.
An IHTMLInputElement is an Input element on HTML Page.
If the IHTMLInputElement does not have name attributes, we will fetch the ID attribute. Then pop up the value.
In conclusion, its really easy to handle BHO with C#. Thats why many there are so many IE add-ons. I hope these are useful to you. To save your time, you can use the project template I made. Download it and put it under your Visual Studio 2005\Templates\ProjectTemplates folder (its usually under My Document).
License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here