Click here to Skip to main content
14,835,793 members
Articles / Programming Languages / C# 3.0
Posted 19 Mar 2012

Tagged as


27 bookmarked

BHO Development using managed code

Rate me:
Please Sign up or sign in to vote.
4.64/5 (10 votes)
21 Mar 2012CPOL4 min read
How to create a simple BHO using C#.


Browser Helper Object (BHO) is a plug-in for Internet Explorer (IE). BHO let developers to drive IE. A plug-in is a program which extends the functionality of a browser. It can be used to retrieve information or modify the content of the webpage that is being displayed in a browser window, or it may just be used to provide the user an option to see the day's Stock market status or, weather in a toolbar.


To start BHO development can be depressing at the very first beginning to learn all those things. As a beginner I want to share my experience to other beginners. Here I am going to explain the simple implementation of BHO.

A Browser Helper Object is a COM object loaded for each IE window. As a browser window is opened, it creates its own copy of the BHO; and, when the window is closed, it destroys its copy of the BHO. You will need a COM DLL which interact with browser. This need to done by implementing the IObjectWithSite in class. We need to use COM interop library to implement COM DLL in our .NET project.

While we are writing in C#, we also need to write the interface IObjectWithSite ourselves. Also, we have to then implement the interface in your BHO. To interact with the HTML document, we will need to add a reference to the Microsoft.mshtml library and to get the DOM or the webpage currently in the browser, we will have to add SHDocVw library as a reference. Also, we will have to add 2 functions which will register (and unregister) our COM component as a BHO with Internet Explorer with the key.

Say Hello to BHO development world: 

Let's create a Hello world project regarding BHO. Start a new C# class library project. I named it as 'HelloBHOWorld'. 

Rename the 'Class1.cs' as 'IObjectWithSite.cs'. 

Add 'using System.Runtime.InteropServices;' to the class. We need to implement the interface 'IObjectWithSite' under this class and we also need to add to function 'GetSite' and 'SetSite'. To know more about this interface, please refer to

IObjectWithSite Members

GetSiteGets the last site set with IObjectWithSite::SetSite. If there is no known site, the object returns a failure code.
SetSiteProvides the site's IUnknown pointer to the object.

Under the IObjectWithSite.cs, we need to point out the GUID of IE for our program, so it can attach to IE. The code snippet should look like the following,

using System.Runtime.InteropServices;
namespace HelloBHOWorld
    //GUID reference of IF
    //Declaration of the interface
        int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
        int GetSite(refGuid guid, outIntPtr ppvSite);

We have done with IObjectWithSite.cs. New we need to add another class file named BHO.cs. Add a class BHO there. As we mentioned before we need to have two references, SHDocVw.dll and MSHTML.dll. SHDocVw is Microsoft Shell Doc Object and Control Library. MSHTML is the 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. To know more, please refer to:

Let's add them,

We are going to use messagebox to display information, so we need to add another reference,

We need to add the following reference under BHO.cs file,

using SHDocVw;
using mshtml;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;

We will need to implement the inerface on BHO class, later we will define the two method.

We need to assign a GUID for our own program under BHO.cs. We can use the System.Guid.NewGuid() method to get a new key (I have a small tool to generate GUID). And also we will add two variables into the class, WebBrowser and HTMLDocument.

In BHO.cs file, we need to write two functions for register/unregister of this DLL.

public static string BHO_KEY_NAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
public static void RegisterBHO(Type type)
    RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHO_KEY_NAME, true);
    if (registryKey == null)
        registryKey = Registry.LocalMachine.CreateSubKey(BHO_KEY_NAME);
    string guid = type.GUID.ToString("B");
    RegistryKey ourKey = registryKey.OpenSubKey(guid);
    if (ourKey == null)
        ourKey = registryKey.CreateSubKey(guid);
    ourKey.SetValue("Alright", 1);

public static void UnregisterBHO(Type type)
    RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHO_KEY_NAME, true);
    string guid = type.GUID.ToString("B");
    if (registryKey != null)
    registryKey.DeleteSubKey(guid, false);

Here we will use OnBeforeNavigate() method to retrieve some information from browser. 'OnBeforeNavigate' invokes an event before navigation occurs on the web browser. To know more please refer,

So next we write the method body of OnBeforeNavigate method and define the 'SetSite' and GetSite' method. On 'SetSite' method we write the event handler for OnBeforeNavigate function.

public void OnBeforeNavigate2(object pDisp, ref object URL, refobject Flags, 
       ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
#region IObjectWithSite Members
public int SetSite(object site)
    if (site != null)
        webBrowser = (WebBrowser)site;
        webBrowser.BeforeNavigate2 += new
        webBrowser.BeforeNavigate2 -= new
        webBrowser = null;
    return 0;

public int GetSite(ref Guid guid, out IntPtr ppvSite)
    IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
    int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
    return hr;

For example we write some code for method 'OnBeforeNavigate2'. This code will track the input field of the site and show input fields name and value.

public void OnBeforeNavigate2(object pDisp, ref object URL, ref object Flags, 
       ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
    document = (HTMLDocument)webBrowser.Document;
    System.Windows.Forms.MessageBox.Show("URL to redirect: " + URL.ToString());
    foreach (IHTMLInputElement tempElement in document.getElementsByTagName("INPUT"))
        System.Windows.Forms.MessageBox.Show( + " = " + tempElement.value);

Register and unregister BHO:

To register/unregister BHO we use 'RegAsm.exe' and write to bat file 'registercom.bat' and 'unregisterall.bat':

REM register for com so we can test the register/unregister functions while debugging
regasm.exe /codebase "HelloBHOWorld.dll"
REM unregister HelloBHOWorld.dll for COM
regasm.exe /unregister "HelloBHOWorld.dll"

Run the file 'registercom.bat'

Now open IE browser and go to 'Tools'>'Manage Add-ons…' You should see your BHO is on the list

Now browse any site (, input text on the box and click button. You should see,

So this can be your first add-in for IE. This is just the beginning, long way to go for writing a professional BHO. Let's hope for the best.

Please don't forget to drop your comments……………..


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Bangladesh Bangladesh
Software development is my passion and profession. Like to take the technical challenges. I try to avoid sticking to a single domain, or methodology. I believe in 'learning concepts rather than learning formulas'. I work with C#,,, Drupal, Java, Blackberry Java development, Android based mobile application, windows mobile application.

My interest is in basic science, reading novel, music, movie. Great fan of the TV channel, ‘National Geography’ and ‘Discovery’.

See my Profile on:

Comments and Discussions

QuestionHow to generate installer for this bho Pin
Member 1373416111-Apr-18 23:45
MemberMember 1373416111-Apr-18 23:45 
QuestionPlugin doesn't work Pin
Member 1267135316-Aug-16 21:42
MemberMember 1267135316-Aug-16 21:42 
QuestionSolution for unresolvable reference to IObjectWithSite Pin
hzzasdf7-Jan-16 5:25
Memberhzzasdf7-Jan-16 5:25 
QuestionBHO Addon got them added in both 32 bit and 64 bit IE but nothing is happening Pin
Member 430162424-Mar-15 9:49
MemberMember 430162424-Mar-15 9:49 
QuestionDoesn't work for me in IE 11 Pin
Vignesh Miriyala25-Feb-15 12:49
MemberVignesh Miriyala25-Feb-15 12:49 
AnswerRe: Doesn't work for me in IE 11 Pin
iffi9929-Jan-20 3:39
Memberiffi9929-Jan-20 3:39 
GeneralMy vote of 1 Pin
BeneGal10-Sep-13 4:42
MemberBeneGal10-Sep-13 4:42 
GeneralRe: My vote of 1 Pin
enamur10-Sep-13 11:12
Memberenamur10-Sep-13 11:12 
Question[My vote of 1] Interesting Pin
junkmegently19-Jun-13 21:13
Memberjunkmegently19-Jun-13 21:13 
Questionmodify html Pin
net2055-Oct-12 3:43
professionalnet2055-Oct-12 3:43 
AnswerRe: modify html Pin
enamur8-Oct-12 1:16
Memberenamur8-Oct-12 1:16 
QuestionRe: modify html Pin
net20512-Oct-12 19:32
professionalnet20512-Oct-12 19:32 
GeneralMy vote of 5 Pin
Manfred Rudolf Bihy21-Sep-12 3:23
professionalManfred Rudolf Bihy21-Sep-12 3:23 
Suggestionmore picture can't show on the page. Please check the picture's URL. Pin
ZhouFuqiang18-Jul-12 22:12
MemberZhouFuqiang18-Jul-12 22:12 
QuestionBHO work with silverlight object Pin
GaurangPatel11-Apr-12 23:11
MemberGaurangPatel11-Apr-12 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.