VS2005 – Browser Helper Object (BHO) Tutorial
I have been dabbling with BHO’s for some time, way back with VB6, then tried in .NET, and with C++ as well. There are so many cool things you can do with them. Anyways, most of the documentation out there is sparse and old. I told myself the next time I have to make one, I am going to document it. Well, here it is in all its glory. Sorry if the code formatting is wacked, but you get the picture. I don’t claim to be an expert, but this should work :)
just in case, I uploaded it in txt format for better reading here
How to Create a Browser Helper Object in Visual Studio 2005 with C++
———————————————————————
1) Open Visual Studio 2005
2) File->New->Project
3) Visual C++
4) ATL
5) ATL Project
6) Name is whatever you want for this example I use “Company.Browser.Helper” without the quotes
7) The ATL Project Wizard Screen will appear, Hit Finish
8) Visual Studio will load up your project.
9) Right Click on The Company.Browser.Helper project, Add->Class
10) select ATL Simple Object, and click the Add button
11) fill in the ShortName – “BrowserHelper” without the quotes, the rest of the fields should fill in, hit next
12) IMPORTANT: Under Support: Check all boxes (ISupportErrorInfo, Connection points, IObjectWithSite (IE object Support)
13) Click Finish
Now on to the better stuff,
————————————————————–
In visual studio, Solution Explorer, Resource Files, you will see BrowserHelper.rgs, open it and add this to the bottom
(replace the GUID with the GUID that you see at the top of the file like CLSID = s ‘{GUID}’) in the other script code
This will register the BHO with IE when the DLL gets registered
HKLM
{
SOFTWARE
{
Microsoft
{
Windows
{
CurrentVersion
{
Explorer
{
'Browser Helper Objects'
{
{GUID}
}
}
}
}
}
}
}
above where it says ‘Browser Helper’ – you can change those names to be more descriptive, that will show in IE add on manager
————————————————————–
————————————————————–
// in the Header Files -> stdafx.h, the bottom part of the file should look like this:
#include "resource.h"
#include
#include
#include
#define CAtlString CString
using namespace ATL;
--------------------------------------------------------------
//in Source Files-> BrowserHelper.cpp you need to include
#include
--------------------------------------------------------------
Then you need to implement methods listed here. You really shouldnt have to modify these
// this should be created by the wizard
STDMETHODIMP CBrowserHelper::InterfaceSupportsErrorInfo(REFIID riid)
{
static const IID* arr[] =
{
&IID_IBrowserHelper
};
for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
if (InlineIsEqualGUID(*arr[i],riid))
return S_OK;
}
return S_FALSE;
}
// the following 4 methods you need to create
STDMETHODIMP CBrowserHelper::SetSite(IUnknown *pUnkSite)
{
CComQIPtr WebBrowser2Ptr;
WebBrowser2Ptr = pUnkSite;
if (WebBrowser2Ptr == NULL)
{
return E_INVALIDARG;
}
// Retrieve and store the IConnectionPointerContainer pointer
m_CPCptr = WebBrowser2Ptr;
if (m_CPCptr == NULL)
{
return E_POINTER;
}
return IEAdvise();
}
STDMETHODIMP CBrowserHelper::IEAdvise(void)
{
HRESULT hr;
CComPtr spCP;
// Receives the connection point for WebBrowser events
hr = m_CPCptr->FindConnectionPoint(DIID_DWebBrowserEvents2, &spCP);
if (FAILED(hr))
{
return hr;
}
// Subscribe the event handlers to the container
hr = spCP->Advise(reinterpret_cast(this), &m_dwCookie);
return hr;
}
STDMETHODIMP CBrowserHelper::IEUnAdvise(void)
{
HRESULT hr;
CComPtr spCP;
// Receives the connection point for WebBrowser events
hr = m_CPCptr->FindConnectionPoint(DIID_DWebBrowserEvents2, &spCP);
if (FAILED(hr))
{
return hr;
}
// Unsubscribe the event handlers to the container
hr = spCP->Unadvise(m_dwCookie);
return hr;
}
STDMETHODIMP CBrowserHelper::IEQuit(void)
{
return IEUnAdvise();
}
------------------------------------------------------------------------------------------
//then you need to implement the invoke method, as you can see i just care about the IEBeforeNaviate2() method
//so i commented the rest of the case statements out but left them in case i need them
STDMETHODIMP CBrowserHelper::Invoke(DISPID dispidMember, REFIID riid,
LCID lcid, WORD wFlags,
DISPPARAMS* pDispParams,
VARIANT* pvarResult,
EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
if (!pDispParams)
{
return E_INVALIDARG;
}
switch(dispidMember)
{
case DISPID_BEFORENAVIGATE2: // Before Naigation
IEBeforeNavigate2(pDispParams);
break;
// case DISPID_COMMANDSTATECHANGE:// Command state change
// IECommandStateChange(pDispParams);
//break;
// case DISPID_DOCUMENTCOMPLETE: // Document completed
// IEDocumentComplete(pDispParams);
//break;
// case DISPID_DOWNLOADCOMPLETE: // Download completed
// IEDownloadComplete(pDispParams);
//break;
// case DISPID_NAVIGATECOMPLETE2: // Navigation completed
// IENavigateComplete2(pDispParams);
//break;
// case DISPID_NEWWINDOW2: // Open a new window
// IENewWindow2(pDispParams);
//break;
// case DISPID_PROGRESSCHANGE: // The progress status change
// IEProgressChange(pDispParams);
//break;
// case DISPID_STATUSTEXTCHANGE: // The status bar text change
// IEStatusTextChange(pDispParams);
//break;
// case DISPID_TITLECHANGE: // Title change
// IETitleChange(pDispParams);
//break;
// case DISPID_ONQUIT: // Quit
// IEQuit();
//break;
}
return S_OK;
}
------------------------------------------------------------------------------------------
//then implement BeforeNavigate2 - you can see the relevant code between the ***** that will
//check if IE is on google and redirect them to yahoo. You can add more code here like checking registry for a key,
//launching a process, etc, etc
void CBrowserHelper::IEBeforeNavigate2(DISPPARAMS* pDispParams)
{
CComQIPtr WebBrowser2Ptr;
CAtlString url;
VARIANT_BOOL* ptrBoolCancel;
VARTYPE vt;
// Check the type of IWebBrowser2
vt = pDispParams->rgvarg[6].vt;
if(vt == 0x0009)
{
WebBrowser2Ptr = pDispParams->rgvarg[6].pdispVal;
}
else
{
// Wrong type, return.
return;
}
// Check the first parameter type is VT_BYREF|VT_BOOL or not
vt = pDispParams->rgvarg[0].vt;
if(vt == 0x400B)
{
ptrBoolCancel = pDispParams->rgvarg[0].pboolVal;
}
else
{
// Wrong type, return.
return;
}
// Check the URL parameter type
vt = pDispParams->rgvarg[5].vt;
if(vt == 0x400C)
{
USES_CONVERSION;
url = OLE2T(pDispParams->rgvarg[5].pvarVal->bstrVal);
}
else
{
// Wrong type, return.
return;
}
// *********************************************************
// check for a given URL
if(url == _T("http://www.google.com/") || url == _T("http://www.google.com"))
{
BSTR newUrl = _T("http://www.yahoo.com/");
*ptrBoolCancel = TRUE;
WebBrowser2Ptr->Navigate(newUrl,NULL,NULL,NULL,NULL);
}
// ********************************************************
}
----------------------------
//you will need to wire these up in your header file, so in HEader Files->BrowserHelper.h it would look like this
----------------------------
// BrowserHelper.h : Declaration of the CBrowserHelper
#pragma once
#include "resource.h" // main symbols
#include "ExDisp.h"
#include "CompanyBrowserHelper.h"
#include "_IBrowserHelperEvents_CP.h"
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif
// CBrowserHelper
class ATL_NO_VTABLE CBrowserHelper :
public CComObjectRootEx,
public CComCoClass,
public ISupportErrorInfo,
public IConnectionPointContainerImpl,
public CProxy_IBrowserHelperEvents,
public IObjectWithSiteImpl,
public IDispatchImpl
{
public:
CBrowserHelper()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_BROWSERHELPER)
BEGIN_COM_MAP(CBrowserHelper)
COM_INTERFACE_ENTRY(IBrowserHelper)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(ISupportErrorInfo)
COM_INTERFACE_ENTRY(IConnectionPointContainer)
COM_INTERFACE_ENTRY(IObjectWithSite)
END_COM_MAP()
BEGIN_CONNECTION_POINT_MAP(CBrowserHelper)
CONNECTION_POINT_ENTRY(__uuidof(_IBrowserHelperEvents))
END_CONNECTION_POINT_MAP()
public:
// ISupportsErrorInfo
STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
//
// IDispatch Methods
//
STDMETHOD(Invoke)(DISPID dispidMember,REFIID riid, LCID lcid,
WORD wFlags, DISPPARAMS* pdispparams,
VARIANT* pvarResult, EXCEPINFO* pexcepinfo,
UINT* puArgErr);
//
// IOleObjectWithSite Methods
//
STDMETHOD(SetSite)(IUnknown *pUnkSite);
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
public:
private:
CComQIPtr m_CPCptr;
DWORD m_dwCookie; // Connection Token - used for
// Advise and Unadvise
enum ConnectType { Advise, Unadvise }; // What to do when managing
// the connection
STDMETHOD(IEAdvise)(void);
STDMETHOD(IEUnAdvise)(void);
STDMETHOD(IEQuit)(void);
void IEBeforeNavigate2(DISPPARAMS* pDispParams);
};
OBJECT_ENTRY_AUTO(__uuidof(BrowserHelper), CBrowserHelper)
----------------------------
And Finally,
To Test:
you shouldnt have to create or modify any more code, if you build it, you should get a dll
then to register with the system you need to call
regsvr32 DllName.dll
now open a browser and goto http://www.google.com and it will redirect to yahoo!
and if you look at the loaded add-ons, it should be in the list
close all your browsers
and to unregister
regsvr32 /u DllName.dll
open a browser and try google, it should work as normal
-------------------------------------------------------------
