Introduction to BHO writing with VB (Browser auxiliary object)

Source: Internet
Author: User

Recently I have been thinking about a special effect. How to drag a file to a webpage? The browser does not automatically jump to the page and reads the file information. haha, I originally wanted to use JavaScript, but I thought about it. There is really no clue. It seems that JavaScript alone is hard to achieve interaction between the browser and the system, so I think of BHO, I have never paid much attention to it before, but it seems that this time it will depend on him.

I think everyone is always interested in some special effects. For example, when a webpage is opened, an advertisement or a link on the webpage will pop up, a program (such as thunder) will be automatically opened ), so how are they implemented? Many of them are inseparable from BHO.

For BHO, you can go to Baidu and I will not elaborate on it here. Let's talk about how to use VB to write BHO. In fact, most BHO is implemented using C ++ or Delphi, this is not the long term of VB, but it is not impossible. If VB is used, it is com:

Use a text editor to write an odl file and save it as iobjectwithsitetlb. odl. The content is as follows:

[
UUID (CF9D9B76-EC4B-470D-99DC-AEC6F36A9261 ),
Helpstring ("VB iobjectwithsite interface "),
Version (1.0)
]
Library iobjectwithsitetlb
{
Importlib ("stdole2.tlb ");
Typedef [public] Long guidptr;
Typedef [public] Long voidptr;
[
UUID (00000000-0000-0000-c000-000000000046 ),
Odl
]
Interface iunknownvb
{
Hresult QueryInterface (
[In] guidptr priid,
[Out] voidptr * pvobj
);
Long addref ();
Long release ();
}
[
UUID (FC4801A3-2BA9-11CF-A229-00AA003D7352 ),
Odl
]
Interface iobjectwithsite: iunknown
{
Typedef iobjectwithsite * lpobjectwithsite;
Hresult setsite ([in] iunknownvb * psite );
Hresult getsite ([in] guidptr priid, [In, out] voidptr * ppvobj );
}
}

The Code generally refers to importing data into the database and declaring interfaces. For details, refer to odl syntax.

Drag this file to mktyplib. EXE (this file should be in VB tools and may not be downloaded from the Internet) and write down the generated iobjectwithsitetlb. TLB file address.

Open VB6, create an ActiveX DLL, reference Microsoft Internet controls and

The iobjectwithsitetlb. TLB you just generated.

Write the following code:

Implements iobjectwithsitetlb. iobjectwithsite
Dim withevents ie as internetexplorer
Dim m_site as iunknownvb

Private sub ie_beforenavigate2 (byval Pdisp as object, URL as variant, flags as variant, targetframename as variant, postdata as variant, headers as variant, cancel as Boolean)
If instr (ie. locationurl, "http: //") then
If instr (CSTR (URL), ":/") then
Cancel = true
Ie. Document. Body. innerhtml = ie. Document. Body. innerhtml + CSTR (URL)
End if
End ifend sub

Private sub iobjectwithsite_getsite (byval priid as iobjectwithsitetlb. guidptr, ppvobj as iobjectwithsitetlb. voidptr)
M_site.queryinterface priid, ppvobj
End sub

Private sub iobjectwithsite_setsite (byval psite as iobjectwithsitetlb. iunknownvb)
Set m_site = psite
Set Ie = psite
End sub

The first line is iobjectwithsitetlb. iobjectwithsite interface inheritance, the other is the definition type or instantiation. Note that the code in private sub ie_beforenavigate2 is the code that I want to process the file and drag it to the webpage. Note that the URL is the address to be redirected, here is the file path of the file you drag.

Compile it into a DLL file and register it with regsvr32. After registration, find the clisd of the DLL you registered under hkcr/clisd/in the registry, in HKEY_LOCAL_MACHINE/software/Microsoft/Windows/CurrentVersion/Explorer/Browser Helper Objects/, create a new item named clisd of the DLL, when you open the browser again, you will see the effect.

However, the process from registration to clisd and creation is not a bit troublesome. In fact, these can be done using code. Below I will provide the code to solve the problem at one time:

Create an EXE project in VB6 and reference the Windows Script Host object model object,

Then write the following code:

Dim wsh as new wshshell
Dim TA as new TLI. tliapplication, Ti as TLI. typelibinfo, TC as TLI. coclassinfo
Private declare function OpenProcess lib "Kernel32" (byval dwdesiredaccess as long, byval binherithandle as long, byval dwprocessid as long) as long
Private declare function waitforsingleobject lib "Kernel32" (byval hhandle as long, byval dwmilliseconds as long) as long

Private const process_all_access = & h1f0fff
Private sub form_load ()
On Error resume next
Me. Visible = false
Dim Cl as string, PID as long, H as long
CL = command
PID = shell ("CMD/C regsvr32/s" + Cl, vbhide)
H = OpenProcess (process_all_access, false, pid)
Waitforsingleobject H,-1
CL = Replace (CL ,"""","")
Set Ti = TA. typelibinfofromfile (CL)
Dim clisd as string
For each TC in Ti. coclasses
Clisd = tc. guid
Wsh. regwrite "HKLM/software/Microsoft/Windows/CurrentVersion/Explorer/Browser Helper Objects/" + clisd + "/", null
Next
Msgbox "complete"
End
End sub
The code is all about com calls. It is easy to understand. compile it into exe. When using it, you only need to drag the DLL file to this EXE and it will automatically register your dll as BHO, then you can open the browser. It's very convenient!

When you want to delete this BHO, just click the tool in the toolbar of the browser ----- Manage Add-on ----- enable or disable the add-on, find the BHO you want to disable, and click Disable. you can also directly Delete the clisd of the corresponding DLL under HKLM/software/Microsoft/Windows/CurrentVersion/Explorer/Browser Helper Objects/in the registry.

If you have any good methods and experience when writing BHO in VB, I hope you can share it with us. If your skills are limited and your code is not correct, I would like to give you some advice.

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.