C # BHO

Source: Internet
Author: User

BHO (Browser help objects) is a COM component that implements a specific interface (iobjectwithsite. In addition to registering as a COM server in the Registry, the developed BHO plug-in must also include its CLSID in hklmsoftware... Register as a subkey under Browser Helper Objects. When the browser [1] starts, it first checks whether a registered bho clsid exists in the registry. If yes, it creates an instance and initializes the BHO instance. The BHO instance runs in the address space of the browser and can perform any operation on accessible objects (such as windows and modules) because it is attached to the main window of the browser, therefore, the lifecycle is the same as that of the browser instance. Demonstrate the BHO creation process:

The following describes how to develop the BHO plug-in. Create a C # project with the class library type. Rename class1.cs to iobjectwithsite. CS, and add two functions to iobjectwithsite: getsite and setsite.

Public interface iobjectwithsite
{
[Preservesig]
Int setsite ([externalas (unmanagedtype. iunknown)] object site );
[Preservesig]
Int getsite (ref guid, out intptr ppvsite );
}

 

Add a CS file urltrack. CS and implement the iobjectwithsite interface. To use BHO, you also need to add two references to shdocvw. dll and mshtml. dll, which can be found in the windowssystem32 directory.

In iobjectwithsite. CSProgramIndicates the guid of IE so that it can be attached (attach) to IE.
[
Comvisible (true ),
Interfacetype (cominterfacetype. interfaceisiunknown ),
GUID ("FC4801A3-2BA9-11CF-A229-00AA003D7352 ")
]

In addition, you also need to assign a guid to the BHO program, which can be obtained through the system. guid. newguid () method.
[
Comvisible (true ),
GUID ("e90da13b-117a-4178-8111-0f712da09ff9 "),
Classinterface (classinterfacetype. None)
]

In urltrack. CS, we also need to write two methods for DLL registration and remove registration.

Public static string bhokeyname = @ "software... Browser Helper Objects ";

[Comregisterfunction]
Public static void registerbho (type)
{
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 bhokey = registrykey. opensubkey (guid, true );
If (bhokey = NULL)
{
Bhokey = registrykey. createsubkey (guid );
}
// Noexplorer: DWORD = 1 prevents the BHO to be loaded by assumer.exe
Bhokey. setvalue ("noexplorer", 1 );
Bhokey. Close ();

Registrykey. Close ();
}

[Comunregisterfunction]
Public static void unregisterbho (type)
{
Registrykey = registry. localmachine. opensubkey (bho_key_name, true );
String guid = type. guid. tostring ("B ");

If (registrykey! = NULL)
Registrykey. deletesubkey (guid, false );
}

 

The next step is to implement specific statistical functions. After entering the URL, we need to record the URL and the current time. When switching the URL in the same browser window, we not only need to record the URL and the current time, you also need to set the end time of the previous browsing record, and also remember the end time when closing the browser. Therefore, you need to mount navigatecomplete2 and onquit events in setsite.

Private void navigatecomplete2 (Object Pdisp, ref object URL)
{
String url = URL as string;
If (URL. indexof ("about: blank")> = 0)
{
Return;
}
If (visithists. Count> 0)
{
Visithist currenthist = visithists [visithists. Count-1];
If (currenthist. visiturl! = URL)
{
Currenthist. endtime = system. datetime. now;
}
Else
{
Return;
}
}
Visithist newhist = new visithist ();
Newhist. starttime = system. datetime. now;
Newhist. visiturl = URL;
Visithists. Add (newhist );
}

Private void onquit ()
{
If (visithists. Count> 0)
{
Visithist currenthist = visithists [visithists. Count-1];
Currenthist. endtime = system. datetime. now;
}

// Output statistical records
...
}

 

Start compilation, and then you can find the DLL file of the project in the bin directory. Use regasm/codebase "urltrack. dll" in the console to register the DLL. Open the registry and go to hklmsoftware... Browser Helper Object can see an extra subitem {E90DA13B-117A-4178-8111-0F712DA09FF9 }.

Note that you must set the comvisvisible attribute in the assemblyinfo. CS file to true. Otherwise, you will get the following information when registering BHO:
Regasm: Warning ra0000: No types were registered.

Related Article

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.