BHO development Basics

Source: Internet
Author: User
Tags http post
Reference: [Reference] | Author: Li hongru

 

 

In the Windows operating system, we have two most common browsers: File Viewer (exploer.exe, which is applied to the file system kernel and internetviewer (I %e.exe, which are used for Internet resources ). These two browsers are powerful and bundled with the Windows operating system, which eventually becomes the browser standard. But sometimes, in order to add some new features to the browser, we often design a new browser. The new browser imitates most of the functions of the standard browser and adds new features. This approach is the most intuitive, but it is actually a lot of work compared with Microsoft's repetitive work. In fact, using the BHO plug-in makes everything easy.

BHO (Browser Help Objects) is a COM component that implements a specific interface. After the developed BHO plug-in is registered at a specific location in the Registry, the BHO instance will be created whenever the Microsoft browser starts. In a browser project, BHO receives many events, such as browsing a new address, moving forward or backward, generating a new window, and exiting the browser; BHO can interact with the browser in the response to these events.

Next, we will first introduce how BHO works. As mentioned above, BHO is a COM component and must implement the IObjectWithSite interface. In addition to registering as a COM Server in the registry, these components must also register their CLSID as a subkey under HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows currentversionpolicerbrowser Helper Objects. Microsoft has reserved space for these components when designing browsers. When the browser starts, the browser first checks whether a registered bho clsid exists in the registry. If yes, it creates an instance, initializes the BHO instance, and establishes an interactive connection. (Note: BHO instances are released only when the browser window for which they are created is destroyed .) Demonstrate the BHO creation process:

The successfully created BHO not only obtains various standard browser operation events and responds to them, but also customizes browser menus, toolbar, and other interface elements. Alternatively, you can install hook functions, monitors every action of the browser. It is worth noting that the use of the BHO plug-in requires an Internet browser of Version 4.0 or later. For a file browser, the operating system requires Windows 95/98/2000 or Windows NT 4.0 or later, and the Shell version is later than 4.71. The following is a list of systems that support the BHO feature:

The Shell operating system version supports BHO.
4.00 Windows 95 and Windows NT 4.0 (IE version 4.0) Only IE4.0
4.71 Windows 95 and Windows NT 4.0 (IE version 4.0) IE and file browser
4.72 Windows 98 IE and file browser
5.00 Windows 2000 IE and file browser

Next, I will introduce how to develop the BHO plug-in. The Development environment is VC6.0 (using ATL) and install the Internet Development SDK in Platform SDK. First, start the atl com AppWizard of VC to generate a project named BhoPlugin, and use the default settings for the rest. Next, let's explain in detail step by step.
Step 1: add an ATL Object to the project. VC menu Insert-> New ATL Object ..., In the displayed dialog box, select "Internet Explorer Object" and enter the COM Class Name (enter EyeOnIE after the Short Name, and other items will be automatically generated ). After that, we can see that the CEyeOnIE class has a base class IObjectWithSiteImpl, which is the template class that implements the IObjectWithSite interface.
Step 2: implement the IObjectWithSite interface method. Before that, we need to define several member variables: CComQIPtr <IWebBrowser2, & IID_IWebBrowser2> mWebBrowser2 (# include "ExDisp. h "), used to save the pointer of the browser component; DWORD mCookie, used to save the connection ID with the browser. IObjectWithSite has two interface methods: SetSite and GetSite. We only need to reload SetSite. Add the function declaration STDMETHOD (SetSite) (IUnknown * pUnkSite) to EyeOnIE. h, and implement the following in EyeOnIE. cpp:
STDMETHODIMP CEyeOnIE: SetSite (IUnknown * pUnkSite)
{
USES_CONVERSION;

If (pUnkSite)
{
MWebBrowser2 = pUnkSite;
If (mWebBrowser2)
{
Return RegisterEventHandler (TRUE );
}
}
Return E_FAIL;
}

HRESULT CEyeOnIE: RegisterEventHandler (BOOL inAdvise)
{
CComPtr <IConnectionPoint> spCP;
// Es the connection point for WebBrowser events
CComQIPtr <IConnectionPointContainer, & IID_IConnectionPointContainer> spCPC (mWebBrowser2 );
HRESULT hr = spCPC-> FindConnectionPoint (DIID_DWebBrowserEvents2, & spCP );
If (FAILED (hr ))
Return hr;

If (inAdvise)
{
// Pass the event handlers to the container
Hr = spCP-> Advise (reinterpret_cast <IDispatch *> (this), & mCookie );
}
Else
{
SpCP-> Unadvise (mCookie );
}
Return hr;
}
We can see that the SetSite parameter actually points to the browser component. In the SetSite implementation, we first save the browser component pointer, and then register the BHO with the browser as an event processor.
Step 3: implement the IDispatch interface method. Event processing is also implemented in IDispatch: Invoke (the ID of each event is defined in ExDispID. h ). BHO may receive many events, but we only need to respond to the part we are interested in. First, add the declaration of this function in EyeOnIE. h. In the implementation of EyeOnIE. cpp, the author tries to respond to the event sent before the browser browses an address.DISPID_BEFORENAVIGATE2To implement simple URL filtering. The Code is as follows:
STDMETHODIMP CEyeOnIE: Invoke (DISPID dispidMember, REFIID riid, LCID lcid,
WORD wFlags, DISPPARAMS * pDispParams,
VARIANT * pvarResult, parameter info * p1_info,
UINT * puArgErr)
{
USES_CONVERSION;

If (! PDispParams)
Return E_INVALIDARG;

Switch (dispidMember)
{
//
// The parameters for this DISPID are as follows:
// [0]: Cancel flag-VT_BYREF | VT_BOOL
// [1]: HTTP headers-VT_BYREF | VT_VARIANT
// [2]: Address of http post data-VT_BYREF | VT_VARIANT
// [3]: Target frame name-VT_BYREF | VT_VARIANT
// [4]: Option flags-VT_BYREF | VT_VARIANT
// [5]: URL to navigate to-VT_BYREF | VT_VARIANT
// [6]: An object that evaluates to the top-level or frame
// WebBrowser object corresponding to the event.
//
Case DISPID_BEFORENAVIGATE2:
{
LPOLESTR lpURL = NULL;
MWebBrowser2-> get_LocationURL (& lpURL );
Char * strurl;
If (pDispParams-> cArgs> = 5 & pDispParams-> rgvarg [5]. vt = (VT_BYREF | VT_VARIANT ))
{
CComVariant varURL (* pDispParams-> rgvarg [5]. pvarVal );
VarURL. ChangeType (VT_BSTR );
Strurl = OLE2A (varURL. bstrVal );
}
If (strstr (strurl, "girl.com "))
{
* PDispParams-> rgvarg [0]. pboolVal = TRUE;
: MessageBox (NULL, _ T ("this webpage has been disabled! "), _ T (" Warning "), MB_ICONSTOP );
Return S_ OK;
}
Break;
}

CaseDISPID_NAVIGATECOMPLETE2:
Break;
CaseDISPID_DOCUMENTCOMPLETE:
Break;
CaseDISPID_DOWNLOADBEGIN:
Break;
CaseDISPID_DOWNLOADCOMPLETE:
Break;
CaseDISPID_NEWWINDOW2:
Break;
CaseDISPID_QUIT:
RegisterEventHandler (FALSE );
Break;
Default:
Break;
}

Return S_ OK;
}
We can see that when the new address browsed by the user contains the "girl.com" character, the browser will pop up a warning dialog box and stop further actions. In addition, in the response to the DISPID_QUIT event (the browser is about to exit), We deregister the BHO event processor.
Step 4, because BHO may be loaded by the file browser. If we do not want to do this, we need to judge the loader in DllMain. For details, refer:
Extern "C"
Bool winapi DllMain (HINSTANCE hInstance, DWORD dwReason, LPVOID/* lpReserved */)
{
If (dwReason = DLL_PROCESS_ATTACH)
{
// Check who's loading us.
// If it's Explorer then "no thanks" and exit...
TCHAR pszLoader [MAX_PATH];
GetModuleFileName (NULL, pszLoader, MAX_PATH );
_ Tcslwr (pszLoader );
If (_ tcsstr (pszLoader, _ T ("assumer.exe ")))
Return FALSE;

_ Module. Init (ObjectMap, hInstance, & LIBID_BHOPLUGINLib );
Disablethreadlibrarycils (hInstance );
}
Else if (dwReason = DLL_PROCESS_DETACH)
_ Module. Term ();
Return TRUE; // OK
}
Finally, do not forget to modify the Registry file and append the registration information of BHO. Add the following code under the EyeOnIE. rgs file:
HKLM
{
SOFTWARE
{
Microsoft
{
Windows
{
CurrentVersion
{
Explorer
{
'Browser Helper objects'
{
{6E28339B-7A2A-47B6-AEB2-46BA53782379}
}
}
}
}
}
}
}
Note: {6E28339B-7A2A-47B6-AEB2-46BA53782379} is the CLSID of the author's BHO. If you develop BHO yourself, enter your CLSID correctly.

BHO plug-ins provide many other functions, such as webpage content analysis and IE interface customization. To sum up, I would also like to remind readers that if you do not want BHO to take effect, you can cancel the plug-in the following format: regsvr32/u yourpathyourbho. dll, or delete the CLSID registered under the "Browser Helper Objects" directory in the registry.
Text complete

Attachment:

  • Description: Createbho.jpg
  • Description: BhoPlugin.zip
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.