Add functionality with Browser Helper Objects
Takeaway: Browser Helper Objects (BHOs) can provide you extra functionality with your Web application beyond normal scripting. Here's how.
This article originally appeared as a Web Development Zone e-newsletter.
By Phillip Perkins
In an intranet setting, you generally have control over the browser that your users will use to access local Web applications. Oftentimes, that browser is Internet Explorer (IE).
This isn't necessarily bad since IE gives developers a plethora of tools for creating robust intranet applications. This article isn't a sales device for IE, but since IE exists and is generally the tool for local intranet applications, it's useful to point out some of the technologies that are available to Web developers.
One of these technologies is Browser Helper Objects (BHO). BHOs are COM objects that run within the process space of IE and can perform any action on the available windows and modules. BHOs can provide you extra functionality with your Web application beyond normal scripting.
Let's say that you have created different applications that require user input, and some of these applications require the same information. This would require that the user enter the data numerous times, possibly creating erroneous data.
But what if you could automatically enter that data when the page loads, and you could store this information on a user's computer, creating a central repository for this information? One piece of information that may fall into this category is the user's personal information such as address, phone number, etc. When the page that contains the form input loads, your BHO will automatically enter the data into the form fields.
I'll be using Visual Basic (VB) to create this component. However, in order to provide the interface so IE can communicate with my component, I must make reference to a type library that exposes the IObjectWithSite interface. Since this isn't readily available, I have to create one. I'll do this using Object Description Language (ODL) and the mktyplib tool that ships with VB. Create a text file called VBBHO.ODL and enter the following code. Click here.
Save this file and use the mktyplib tool to create the type library file. Open the Command Prompt, navigate to the directory that contains MKTYPLIB.EXE, and enter mktyplib c:\[path to ODL file]\vbbho.odl. Create a new ActiveX DLL application in VB. Name the project VBBHO and name the class module MyBHO. Open the project references, click on the browse button, and add the VBBHO.TLB file that we just created. Also, make reference to Microsoft XML v2.6 or above, Microsoft Internet Controls, and Microsoft HTML Object Library.
Add the following code to your class module. Click here.
When IE starts up, it will create an instance of your object and will first call the SetSite() method. A pointer to the InternetExplorer object is passed in and stored both to the m_Site and m_ie member variables. The m_Site member variable is used to pass back a reference in the GetSite() method.
The most important part of this code is the DocumentComplete event of the m_ie member variable. When this event fires (when the page is completed loading), each myInput INPUT element is looped and the value attribute is set. The input tag will look something like this:
<INPUT TYPE="text" NAME="myInput" field="//personal_info/first_name">
This piece of code also loads a file called data.xml, which is located in the same directory as the component. Here's the XML for that file:
<?xml version='1.0'?>
<xml>
<personal_info>
<first_name>John</first_name>
<last_name>Public</last_name>
<age>99</age>
</personal_info>
</xml>
In order for IE to start up the completed
component, you need to add a key to the registry under
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\
CurrentVersion\Explorer\Browser Helper Objects. If the Browser
Helper Objects key is not present, you will have to add it. Compile
your component and find the CLSID of your component in the registry
under HKEY_CLASSES_ROOT\VBBHO.MyBHO. Copy the CLSID of the
component and add it to the Browser Helper Objects key. You can
also add a MsgBox call in your component code within the SetSite()
method to make sure that it loaded.
Here's some HTML to test your component once you've determined that it's loading. Click here.
Load this page in IE, and you should see these INPUT fields automatically loaded with the information in the XML file.
Note: Editing the registry is risky. Before making any changes, back up the registry so you can restore it if something goes wrong.
Phillip Perkins is a contractor with Ajilon Consulting. His experience ranges from machine control and client/server to corporate intranet applications.
SponsoredWhite Papers, Webcasts, and Downloads
- IP Telephony from A to Z: The Complete IP Telephony eBook ShoreTel
- ShoreTel Ergonomic Phones ShoreTel
- How Business Networks are Evolving Today SAP
- Accelerating Network-based Backup Riverbed
- Riverbed's "Jack" Product Demo: The Most Complete WAN Acceleration Solution Riverbed
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
