BHO (Browser Helper Object)
BHO is an industry standard launched by Microsoft to open interactive interfaces to third-party programmers as a browser. with simple code, BHO can access interactived interfaces in the browser field ). Through this interface, programmers can write code to obtain browser behaviors, such as "backward", "forward", and "Current page". Using the Interaction Features of BHO, programmers can also use code to control browser behavior, such as modifying and replacing the browser toolbar and adding their own program buttons.
-------------- Imported from Baidu Baike BHO
To create a BHO (using C ++) in Vs, follow these steps:
1. Create an ATL project named bhodemo
2. After the project is created, switch to the Class View and add an ATL simple object mybho. As shown in the Wizard, select iobjectwithsite.
3. we can see that an interface implementation class cmybho is generated, and an inherited parent class is added to it: Public idispeventimpl <1, cmybho, & diid_dwebbrowserevents2, & libid_shdocvw, 1, 1> and two methods
Stdmethod (setsite) (iunknown * punksite); // override the iobjectwithsiteimpl method of the parent class
Void stdmethodcalltype ondocumentcomplete (idispatch * Pdisp, variant * URL); // action when the file is loaded
4. Add the event receiver section:
Begin_sink_map (cmybho)
Sink_entry_ex (1, diid_dwebbrowserevents2, dispid_documentcomplete, ondocumentcomplete)
End_sink_map ()
At this time, the first parameter 1 must be consistent with the value of 1 in the parameter following idispeventimpl. in this way, when the dispid_documentcomplete message is transmitted from IE to BHO, the corresponding ondocumentcomplete will be called. it can inherit multiple idispeventimpl, but make sure that the first parameter in the parameter, that is, the sequence number is different.
5. Implement setsite and ondocumentcomplete
Because no more operations are performed yet, setsite directly calls iobjectwithsiteimpl <cmybho>: setsite (punksite), while ondocumentcomplete is empty first.
The current source file should look like this:
1: // mybho. h: cmybho statement
2:
3: #pragma once
4: # include "resource. H" // main symbol
5: #include "ExDispid.h"
6:
7: #include "BHODemo_i.h"
8:
9: #if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
10: # error "A single-thread COM object cannot be correctly supported on Windows CE platform (for example, Windows Mobile platform that does not provide full DCOM Support. Define _ ce_allow_single_threaded_objects_in_mta to force ATL to support creating a single-thread COM Object and using its single-thread COM object. The thread model in the RGS file has been set to "free" because the model is not the only thread model supported by the DCOM Windows CE platform. "
11: #endif
12:
13: using namespace ATL;
14:
15: // CMyBHO
16:
17: class ATL_NO_VTABLE CMyBHO :
18: public CComObjectRootEx<CComSingleThreadModel>,
19: public CComCoClass<CMyBHO, &CLSID_MyBHO>,
20: public IObjectWithSiteImpl<CMyBHO>,
21: public IDispatchImpl<IMyBHO, &IID_IMyBHO, &LIBID_BHODemoLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
22: public IDispEventImpl<1 ,CMyBHO,&DIID_DWebBrowserEvents2,&LIBID_SHDocVw,1 ,1 >
23: {
24: public:
25: CMyBHO()
26: {
27: }
28:
29: DECLARE_REGISTRY_RESOURCEID(IDR_MYBHO)
30:
31: BEGIN_COM_MAP(CMyBHO)
32: COM_INTERFACE_ENTRY(IMyBHO)
33: COM_INTERFACE_ENTRY(IDispatch)
34: COM_INTERFACE_ENTRY(IObjectWithSite)
35: END_COM_MAP()
36:
37: // route table
38: BEGIN_SINK_MAP(CMyBHO)
39: SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)
40: END_SINK_MAP()
41:
42: DECLARE_PROTECT_FINAL_CONSTRUCT()
43:
44: HRESULT FinalConstruct()
45: {
46: return S_OK;
47: }
48:
49: void FinalRelease()
50: {
51: }
52:
53: public:
54: stdmethod (setsite) (iunknown * punksite); // override the iobjectwithsiteimpl method of the parent class
55: void stdmethodcalltype ondocumentcomplete (idispatch * Pdisp, variant * URL); // action when the file is loaded
56: };
57:
58: OBJECT_ENTRY_AUTO(__uuidof(MyBHO), CMyBHO)
The CPP file should be like this:
1: // Implementation of mybho. cpp: cmybho
2:
3: #include "stdafx.h"
4: #include "MyBHO.h"
5:
6:
7: // CMyBHO
8: // override the parent class iobjectwithsiteimpl Method
9: STDMETHODIMP CMyBHO::SetSite(IUnknown * pUnkSite)
10: {
11: // call the implementation of the base class.
12: return IObjectWithSiteImpl<CMyBHO>::SetSite(pUnkSite);
13: }
14: void stdmethodcalltype cmybho: ondocumentcomplete (idispatch * Pdisp, variant * URL) // action when the document is loaded
15: {
16: }
6. Registry File
Modify the bhodemo. RGS file, delete the original content, and change it:
HKLM{ NoRemove SOFTWARE { NoRemove Microsoft { NoRemove Windows { NoRemove CurrentVersion { NoRemove Explorer { NoRemove 'Browser Helper Objects' { ForceRemove {E3C599B7-EB1D-462B-9ED8-D3924EF51BE9} = s 'BHO Demo Class' { val NoExplorer = d '1' } } } } } } }}
The value after forceremove is the guid value of mybho In the copied IDL file.
7. Compile and enable IE to check whether bhodemo is enabled. errors may occur during compilation in win7, and permission issues may occur. In this case, you need to run vs as administrator.
Uninstall: directly go to regsvr32/u bhodemo. dll in the DEBUG directory of the Project. Pay attention to the permissions under win7.