Search Hooks : Expediting search in IE4.0

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Environment: VC 6.0, IE 4

Introduction

This article demonstrates the use of URL Search Hooks in IE
4.0. It has been implemented as a simple DLL which was created
using ATL-COM AppWizard and has just one object called SrchHook.
SrchHook implements the Translate function and has a property
"srch".

This article also shows how to use the About URL protocols in
IE40.

How does the browser attempt to open a site?

The browser jumps to a site using the protocol defined in the
URL in the address bar. If it is not able to find the protocol,
it tries to find protocol from the address. (trying to form site
names prefixing www and/sufficing edu/com etc.) If it is not able
to find them, the browser creates Search Hook objects and calls
each of their Translate function.

I have created such a simple object(SrchHook) which implements
this function.

SrchHook performs:

  1. 1) Address translation, and
  2. 2) Setting and getting of the search engine string from
    the registry. (This string is further passed as the final
    URL for the browser to jump to!!)

How is the POST method for search-sites used in this
application?

It was observed that if someone types say "CodeGuru of
the month" in the search text box of a search engine, say
altavista, then the browser jumps to the following URL:
http://www.altavista.com/cgi-bin/query?q=CodeGuru
of the month&text=yes
Similarly, for other search-engines there are different such POST
queries. This implies that the browser attempts to open another
site-URL using the string entered by the user. This is further
used as follows.

Usage in the current application

1. Address Translation
Such a string is stored in the registry for the
selected search engine. This is further used to form the final
URL string passed to the browser by the Translate function: This
function formats the search engine string(from Registry) and
forms the final URL to move to. It returns S_OK for the case that
browser will not form any further objects for performing address
translation and presumes that the address has been interpreted
correctly.

STDMETHODIMP CSrchHook::Translate(LPWSTR lpwszSearchURL, DWORD cchBufferSize)
{
   // Copy the string entered by the user (which is not recognised by IE).
   WCHAR *wpStr = new WCHAR[2048];
   wcscpy(wpStr, lpwszSearchURL);

   //Read the search engine from the registry.
   HKEY hKey;
   if(RegOpenKey(HKEY_LOCAL_MACHINE, _T(szMainKey), &hKey) != ERROR_SUCCESS)
      return S_FALSE;

   TCHAR szKeyBuf[2048];
   long lSize;
   if(RegQueryValue(hKey, NULL, szKeyBuf, &lSize)!= ERROR_SUCCESS)
      return S_FALSE;

   int nSize = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, szKeyBuf, -1, NULL, 0);
   WCHAR *wzKeyBuf = new WCHAR[nSize + 1];
   MultiByteToWideChar(CP_ACP, MB_COMPOSITE, szKeyBuf, -1, wzKeyBuf, nSize);
   wsprintfW(lpwszSearchURL, wzKeyBuf, wpStr);

   delete [] wpStr;
   delete [] wzKeyBuf;

   return S_OK;
} 

This function formats the search engine string(from Registry)
and forms the final URL to move to. It returns S_OK for the case
that browser will not form any further objects for performing
address translation and presumes that the address has been
interpreted correctly.

2) Setting and getting of the search engine string
from the registry
.
The very same object (SrchHook) also has a property called
"srch" which holds the selected search engine string
URL format.

  1. Setting the Search Engine

    The search engine can be set
    by the user by typing "about:searchprovider" in
    the address bar. The user is then taken to a page whereby
    he can select one of the search engines in the combo box
    provided and hitting OK.

    This sets the srch property of the object to the
    selected search engine string which in turn puts this
    string in the registry by calling its method
    SetRegistrySearchProvider.

    The user is then notified by message e.g. "Search
    Provider Set : altavista"

    STDMETHODIMP CSrchHook::put_srch(BSTR newVal)
    {
       /*set the registry string */
       SetRegistrySearchProvider(newVal);
       return S_OK;
    }
    
  2. Getting of search engine is used at the loading time of
    the searchprovider page to display the current value of
    the search engine.

    STDMETHODIMP CSrchHook::get_srch(BSTR *pVal)
    {
       // get the registry string
        GetRegistrySearchProvider(pVal);
        return S_OK;
    } 

AboutURL protocols

The application’s .rgs file was modified for adding an entry
in HKEY_LOCAL_MACHINESOFTWAREMicrosoft’Internet
Explorer’AboutURLs for implementing
"about:searchprovider". This is the place which holds
the locations for other AboutURLs also like
"about:NavigationCanceled" etc. Just the location of
the html file needs to be given here in order to implement this.
In this case I just made the HTML resource as the part of the
application. The browser interprets this and opens the HTML Page
for the search engine selection.

Demo

  • Download the demo project and register this DLL
    ("regsvr32 SearchHook.DLL").
  • Jump to "about:searchprovider" in IE4 and
    choose some search engine there.
  • Now try to jump to some address you want to perform
    search on, say : "taste of india". It shows you
    the search results from the selected search engine.

Usage

This component can serve as a very useful and enhanced method
for searching capability. The user can customise his search
provider and that way the need of visiting the search engine
first is obliviated. The user will not have to jump to search
engines and then perform search. Mere typing in the address bar
will result in showing him results of the search in the engine
choosen by him.

Requirements

Internet Explorer 4.0 and VC 6.0

Notes

Objects of type URLSearch Hooks are entered at
HKEY_CURRENT_USERSoftware Microsoft Internet Explorer
UrlSearchHooks(instead of HKEY_LOCAL_MACHINE….
).

References

Inet Client SDK

Download demo project – 9 KB

Download source – 17 KB

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read