Search Hooks : Expediting search in IE4.0

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_MACHINE\SOFTWARE\Microsoft\'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_USER\Software\ Microsoft\ Internet Explorer\ UrlSearchHooks(instead of HKEY_LOCAL_MACHINE\..\.. ).

References

Inet Client SDK

Download demo project - 9 KB

Download source - 17 KB



Comments

  • How to set Defaults

    Posted by Legacy on 06/17/2003 12:00am

    Originally posted by: Delamare

    Hello.
    I�m get the source and compile OK.
    but in the first time after you register you need to go to
    about:searchprovider and set the searcher.

    how to set the default value to not need to enter in this about:searchprovider, when you register the dll a search provider like yahoo is the default..

    Where I need to change ?

    Regards

    Reply
  • The source not works to compile..

    Posted by Legacy on 06/02/2003 12:00am

    Originally posted by: Leandro

    I�m download a source code, but when I try to build
    I recieve errors

    Someone can help me ?

    SearchHook.cpp
    SrchHook.cpp
    C:\VCPRojects\NOVO\Fonte\SrchHook.cpp(23) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [36]' to 'const unsigned short *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\VCPRojects\NOVO\Fonte\SrchHook.cpp(27) : error C2664: 'Create' : cannot convert parameter 2 from 'char [5]' to 'const unsigned short *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\VCPRojects\NOVO\Fonte\SrchHook.cpp(29) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [36]' to 'const unsigned short *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\VCPRojects\NOVO\Fonte\SrchHook.cpp(33) : error C2664: 'long __stdcall ATL::CRegKey::SetValue(struct HKEY__ *,const unsigned short *,const unsigned short *,const unsigned short *)' : cannot convert parameter 2 from 'char [8]' to 'const unsigned sh
    ort *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\VCPRojects\NOVO\Fonte\SrchHook.cpp(35) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [36]' to 'const unsigned short *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\VCPRojects\NOVO\Fonte\SrchHook.cpp(55) : error C2065: 'LszMainKey' : undeclared identifier
    C:\VCPRojects\NOVO\Fonte\SrchHook.cpp(66) : error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'unsigned short [2048]' to 'const char *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\VCPRojects\NOVO\Fonte\SrchHook.cpp(68) : error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'unsigned short [2048]' to 'const char *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\VCPRojects\NOVO\Fonte\SrchHook.cpp(93) : error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'unsigned short [2048]' to 'const char *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\VCPRojects\NOVO\Fonte\SrchHook.cpp(96) : error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'unsigned short [2048]' to 'const char *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    Generating Code...
    Error executing cl.exe.

    Reply
  • Is there any interface for searching history?

    Posted by Legacy on 04/15/2003 12:00am

    Originally posted by: Subramanya

    In IE 5, if you view history (Ctrl H), the history pane displays search option. Upon typing a keyword and clicking search it displays a set of results in alphabetical order. How does this work? Where does the IE store these results.

    Thanks in advance

    Reply
  • How Can I Control IE's Location URL

    Posted by Legacy on 11/29/2001 12:00am

    Originally posted by: Tae Jin Yoon


    Exactly I want to do is make BHO component whick is when user write url text box example > tj://abc then go http://www.abc.com/x.dll?GetX&ID=abc.
    First I Think Hook DISPID_BEFORENAVIGATE2 event and
    target url transformation definded url...
    how can I do that??

    help me....

    Reply
  • searchhook

    Posted by Legacy on 07/09/2001 12:00am

    Originally posted by: Rob Canary

    I would like to know if you could instuct me on how to "REMOVE" the searchHook.dll from my windows?

    Reply
  • Why it not work with IE 5.0 ?

    Posted by Legacy on 04/29/2000 12:00am

    Originally posted by: misa

    I have retryed more times,
    but it do not work with IE5.0
    Why?
    Thanks !
    misa

    Reply
  • IE crash on Close, after successfully working with the IUrlSearchHook

    Posted by Legacy on 04/12/2000 12:00am

    Originally posted by: G.Shekter

    The sample compiles o.k (needed a few Unicode tunnings).
    The sample seems to work fine (even via the VC++ 6.0 Debugger) for both the 'SearchProvider' and the 'URLSearchHook'.

    The problem apears occasionally (that's what so funny about it), say 1 in 2 browser instances.
    It goes like that: open a new IE, enter a search string few times. On Close a crash error message appears yielding an Access Violation).
    If you running via the debugger the last lines on the debug pane are:

    .
    .
    .
    Loaded 'E:\temp\searchhook\ReleaseUMinDependency\SearchHook.dll', no matching symbolic information found.
    The thread 0x144 has exited with code 0 (0x0).
    The thread 0x160 has exited with code 0 (0x0).
    The thread 0x167 has exited with code 0 (0x0).
    The thread 0x161 has exited with code 0 (0x0).
    First-chance exception in IEXPLORE.EXE (SHDOCVW.DLL): 0xC0000005: Access Violation.
    The program 'D:\Program Files\Internet Explorer\IEXPLORE.EXE' has exited with code 0 (0x0).


    A hint or any other help will be greatly appreciated.

    Reply
  • It's not work when url incloud DBCS such as Chinese chars

    Posted by Legacy on 01/11/2000 12:00am

    Originally posted by: mark wang

    I find It's not work when url incloud DBCS such as Chinese chars,it then bypass urlsearchhook goto the search in excite
    
    search engine.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • When it comes to desktops – physical or virtual – it's all about the applications. Cloud-hosted virtual desktops are growing fast because you get local data center-class security and 24x7 access with the complete personalization and flexibility of your own desktop. Organizations make five common mistakes when it comes to planning and implementing their application management strategy. This eBook tells you what they are and how to avoid them, and offers real-life case studies on customers who didn't …

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds