Click here to Skip to main content
15,885,366 members
Articles / Desktop Programming / Win32

Writing a BHO in Plain C++

Rate me:
Please Sign up or sign in to vote.
4.88/5 (45 votes)
6 Jun 2009CPOL17 min read 210.4K   10.5K   128  
How to write an Internet Explorer plug-in (Browser Helper Object - BHO) using just C++ and the Windows API; no ATL or MFC involved!
/*
 Copyright (C) 2009 Moutaz Haq <cefarix@gmail.com>
 This file is released under the Code Project Open License <http://www.codeproject.com/info/cpol10.aspx>

 This file contains our implementation of the IClassFactory interface, CClassFactory.
*/

#include "common.h"
#include "ClassFactory.h"
#include "ObjectWithSite.h"

const IID CClassFactory::SupportedIIDs[]={IID_IUnknown,IID_IClassFactory};

// Initialize the IUnknown implementation with our two supported IIDs: IID_IUnknown, and IID_IClassFactory
CClassFactory::CClassFactory() : CUnknown<IClassFactory>(SupportedIIDs,2)
{
}

CClassFactory::~CClassFactory()
{
}

// This is called by COM to create an instance of our main class
STDMETHODIMP CClassFactory::CreateInstance(IUnknown *pUnkOuter,REFIID riid,void **ppvObject)
{
	HRESULT hr;

	// pUnkOuter is non-NULL only when aggregating classes. Since we don't support aggregation, return CLASS_E_NOAGGREGATION if pUnkOuter is non-NULL.
	if(pUnkOuter!=NULL) return CLASS_E_NOAGGREGATION;
	// Check if ppvObject pointer is valid
	if(IsBadWritePtr(ppvObject,sizeof(void*))) return E_POINTER;
	// Set *ppvObject to NULL
	(*ppvObject)=NULL;
	// We only support creating the CObjectWithSite object, which is our implementation of the IObjectWithSite interface through which Internet Explorer will access the BHO.
	CObjectWithSite* pObject=new CObjectWithSite;
	// If we couldn't allocate a new CObjectWithSite object, return an out-of-memory error.
	if(pObject==NULL) return E_OUTOFMEMORY;
	// Query pObject for the requested interface
	hr=pObject->QueryInterface(riid,ppvObject);
	// If the requested interface is not supported by pObject, it will return an error. In that case, delete the newly created object.
	if(FAILED(hr)) delete pObject;
	// Return the same HRESULT as CObjectWithSite::QueryInterface
	return hr;
}

// This is called to lock/unlock our DLL in memory by the host process. While the DLL is locked, it will not be unloaded from memory.
STDMETHODIMP CClassFactory::LockServer(BOOL fLock)
{
	// If locking, increment the DLL reference count
	if(fLock) InterlockedIncrement(&DllRefCount);
	// If unlocking, decrement the DLL reference count
	else InterlockedDecrement(&DllRefCount);
	return S_OK;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions