Click here to Skip to main content
11,706,323 members (59,019 online)
Click here to Skip to main content

Integrate the latest MSDN with VC6 and Hook functions of COM

, 27 Mar 2005 CPOL 151.7K 2.4K 76
Rate this:
Please Sign up or sign in to vote.
Integrate the latest MSDN with VC6 and Hook functions of COM.

Introduction

After Oct. 2001, you cannot get help by pressing F1 in VC6 with latest MSDN. Microsoft has changed the help format from CHM to document explorer.

  • Is it possible to use the latest MSDN in VC6?
  • The answer is YES!

Details

First, we must know how VC6 brings up the help after you press F1. This is easy if you have a debugger such as Soft-ICE.

If you don't install MSDN, after you press F1, VC6 will popup a message box which says you haven't installed MSDN. Set a breakpoint at MessageBox. Press F1 and the debugger will popup. Look at the stack. You will see this function was called from "C:\Program Files\Common Files\Microsoft Shared\VS98\vshelp.dll".

Then we use Dependency Walker ("depends" in Visual Studio Tools) to see what functions are exported. We will see DllRegisterServer and DllUnregisterServer. It's obviously a COM.

Use VC6 to create a simple console project. Add the following line to your .cpp file.

#import "C:\Program Files\Common Files\Microsoft Shared\VS98\vshelp.dll"

And build your project. Open the Debug directory, and you will find "vshelp.tlh" and "vshelp.tli". Open "vshelp.tlh", you will see:

struct __declspec(uuid("854d7ac0-bc3d-11d0-b421-00a0c90f9dc4"))
    IVsHelpSystem : IUnknown
{
    //
    // Wrapper methods for error-handling
    //

    HRESULT KeywordSearch (
        LPWSTR pszKeyword,
        long dwFlags,
        long dwReserved );
    HRESULT ALinkSearch (
        LPWSTR pszALink,
        long dwFlags,
        long dwReserved );
    ...

The function KeywordSearch was called when you press F1. So if we replace this function and call the latest MSDN's help function, it should work.

Then how to do this? Since it's a COM interface, it's easy to hook it.

Basically a COM interface is a C++ class with a virtual table. It has a pointer to a table that contains the address of all virtual functions. And this table is shared by all instances. So we only need to change the function address in the table. And this interface will look like:

struct _IVsHelpSystemVtbl{
    HRESULT (STDMETHODCALLTYPE *QueryInterface)(IUnknown * This, 
      REFIID riid, void **ppvObject);
    ULONG (STDMETHODCALLTYPE *AddRef)(IUnknown * This);
    ULONG (STDMETHODCALLTYPE *Release)(IUnknown * This);
    HRESULT (STDMETHODCALLTYPE *KeywordSearch)(IUnknown * This, 
      LPWSTR pszKeyword, long dwFlags, long dwReserved);
};

struct _IVsHelpSystem
{
    struct _IVsHelpSystemVtbl *lpVtbl;
};

What we need to do now is create an instance of this COM. And we will get the address of the table. Now we need to know how to bring up the help of the latest MSDN. It seems it's not documented. It's also a COM.

Add the following line to your .cpp file. You will get the COM definition.

#import "C:\Program Files\Common Files\Microsoft Shared\MSEnv\vshelp.tlb"

In "vshelp.tlh", you can find function:

HRESULT DisplayTopicFromF1Keyword ( _bstr_t pszKeyword );

Obviously, this is what we need. So in the function KeywordSearch, call DisplayTopicFromF1Keyword which will launch the latest MSDN.

Here is the code to hook the function KeywordSearch:

HRESULT hr = theHelp.CreateInstance(__uuidof(VsHelp::DExploreAppObj));
if (SUCCEEDED(hr))
{
    HRESULT hr = vc6Help.CreateInstance(
      __uuidof(VsHelpServices::VsHelpServices));
    if (SUCCEEDED(hr))
    {
        iHelp = (_IVsHelpSystem *)vc6Help.GetInterfacePtr();

        TRACE1("iHelp = %x\n", iHelp);
        TRACE1("lpVtbl = %x\n", iHelp->lpVtbl);
        TRACE1("KeywordSearch = %x\n", iHelp->lpVtbl->KeywordSearch);

        OldKeywordSearch = iHelp->lpVtbl->KeywordSearch;

        DWORD dwOldProtect;
        if (VirtualProtect(iHelp->lpVtbl, sizeof(
          _IVsHelpSystemVtbl), PAGE_READWRITE, &dwOldProtect))
            iHelp->lpVtbl->KeywordSearch = MyKeywordSearch;
    }
}

Here is the code of MyKeywordSearch. In this function, we will use the help collection specified by Dependency Walker as the default help collection. If default help collection is specified, we will check if it's 'MSDN Online'. If yes, we will call ShellExecute to start a browser to show the help. Otherwise, we will find the help collection's filename from the registry.

HRESULT __stdcall MyKeywordSearch(IUnknown * This, 
             LPWSTR pszKeyword, long dwFlags, long dwReserved)
{
    ASSERT(theHelp != NULL);

    LONG lResult;
    HKEY hKey;
    lResult = RegOpenKeyEx(HKEY_CURRENT_USER,
        "Software\\Microsoft\\Dependency Walker\\External Help",
        0, KEY_READ, &hKey);
    if (lResult == ERROR_SUCCESS)
    {
        CHAR szCollection[MAX_PATH];
        DWORD cbCollection = MAX_PATH;
        lResult = RegQueryValueEx(hKey, "Collection", NULL, 
                  NULL, (LPBYTE)szCollection, &cbCollection);
        if (lResult == ERROR_SUCCESS)
        {
            TRACE1("use collection: %s\n", szCollection);

            if (stricmp(szCollection, "Online") == 0)
            {
                WCHAR szURL[1024];
                DWORD dwURL = sizeof(szURL);
                lResult = RegQueryValueExW(hKey, L"URL", 
                               NULL, NULL, (LPBYTE)szURL, &dwURL);
                if (lResult == ERROR_SUCCESS)
                {
                    WCHAR *p = wcsstr(szURL, L"%1");
                    if (p)
                    {
                        p[1] = L's';
                        WCHAR szLink[1024];
                        if (_snwprintf(szLink, sizeof(szLink)/sizeof(WCHAR), 
                                               szURL, pszKeyword) > 0)
                        {
                            TRACE1("use collection: %S\n", szLink);
                            
                            ShellExecuteW(NULL, L"open", szLink, 
                                          NULL, NULL, SW_SHOWNORMAL);

                            RegCloseKey(hKey);
                            return S_OK;
                        }
                    }
                }
            } /* end Online */
            else
            {
                HKEY hHelp;
                lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                    "SOFTWARE\\Microsoft\\MSDN\\7.0\\Help\\0x0409",
                    0, KEY_READ|KEY_ENUMERATE_SUB_KEYS, &hHelp);
                if (lResult == ERROR_SUCCESS)
                {
                    DWORD dwIndex = 0;
                    CHAR szGuid[MAX_PATH];
                    while (RegEnumKey(hHelp, dwIndex++, szGuid, 
                           MAX_PATH) == ERROR_SUCCESS)
                    {
                        HKEY hGuid;
                        if (RegOpenKeyEx(hHelp, szGuid, 0, 
                                         KEY_READ, &hGuid) != ERROR_SUCCESS)
                            continue;
                        
                        CHAR szCollection2[MAX_PATH];
                        DWORD cbCollection2 = MAX_PATH;
                        lResult = RegQueryValueEx(hGuid, NULL, NULL, 
                                  NULL, (LPBYTE)szCollection2, &cbCollection2);
                        if (lResult == ERROR_SUCCESS && 
                            stricmp(szCollection, szCollection2) == 0)
                        {
                            cbCollection2 = MAX_PATH;
                            lResult = RegQueryValueEx(hGuid, "Filename", 
                                      NULL, NULL, (LPBYTE)szCollection2, 
                                      &cbCollection2);
                            if (lResult == ERROR_SUCCESS &&
                                strnicmp(szCollection2, "ms-help://", 
                                sizeof("ms-help://")-1) == 0)
                            {
                                TRACE1("use collection: %s\n", szCollection2);
                                theHelp->SetCollection(szCollection2, "");
                                
                                RegCloseKey(hGuid);
                                break;
                            }
                        }
                        
                        RegCloseKey(hGuid);
                    } /* end RegEnumKey(hHelp) */
                    
                    RegCloseKey(hHelp);
                }
            } /* end not Online */          
        }

        RegCloseKey(hKey);
    }

    theHelp->SyncIndex(pszKeyword, 1);
    theHelp->DisplayTopicFromF1Keyword(pszKeyword);

    return S_OK;
}

Easy, right?

There are two things you must keep in mind. First, we must declare the vc6Help in global space and release it when the program quits. This is because the vshelp.dll will be freed if there is no more instance. If so, then what we modified will be gone with it. Then second, we must call VirtualProtect to make the virtual table become writable, otherwise you cannot modify the virtual table because it's read only.

Sometimes, you may have a couple of help collections installed. After you press F1, it may not show the help collection you want.

How to set the default help collection?

  1. I believe you have installed the latest Platform SDK. If you don't have one, download it from Microsoft. It's free and useful. You must download and install it.
  2. Run 'Depends.exe' under the SDK\bin directory.
  3. Select menu 'Options'->'Configure External Function Help Collection'.
  4. Choose the help collection you want. You can even choose 'MSDN Online' and this add-in will show online help. This means you can get help without having MSDN installed.

How to use it?

Click VC6's menu "Tools"->"Customize"->"Add-ins and Macro Files"->"Browse". Then choose "VSNetHelp.dll", and click "Close".

Move the caret to a keyword, press F1. You got it!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chunhua Liu
Technical Lead
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
Generalgreat Pin
tzl200024-Apr-08 23:31
membertzl200024-Apr-08 23:31 
QuestionVS 2008 DVD document can't sing F1 Pin
AnEagle24-Mar-08 2:17
memberAnEagle24-Mar-08 2:17 
AnswerRe: VS 2008 DVD document can't sing F1 Pin
morningye17-Jul-09 14:27
membermorningye17-Jul-09 14:27 
Generalnew version!!! Pin
free2000fly31-Dec-07 3:47
memberfree2000fly31-Dec-07 3:47 
GeneralGreate Job Pin
brent_yu7-Nov-07 10:02
memberbrent_yu7-Nov-07 10:02 
GeneralVSNetHelp.dll Bug Pin
adlyer4-Nov-07 4:25
memberadlyer4-Nov-07 4:25 
GeneralDownload link for MSDN 2005 Pin
allting9-Nov-06 19:08
memberallting9-Nov-06 19:08 
GeneralRe: Download link for MSDN 2005 Pin
Deodara15-Jan-07 22:15
memberDeodara15-Jan-07 22:15 
AnswerRe: Download link for MSDN 2005 Pin
YoSilver24-Feb-07 8:15
memberYoSilver24-Feb-07 8:15 
Generalgood idea, but causes too much crashes Pin
PC-Alex11-May-06 19:24
memberPC-Alex11-May-06 19:24 
GeneralRe: good idea, but causes too much crashes Pin
Chunhua Liu8-Jun-06 20:53
memberChunhua Liu8-Jun-06 20:53 
GeneralRe: good idea, but causes too much crashes Pin
PC-Alex8-Jun-06 20:58
memberPC-Alex8-Jun-06 20:58 
GeneralRe: good idea, but causes too much crashes Pin
Chunhua Liu8-Jun-06 21:46
memberChunhua Liu8-Jun-06 21:46 
GeneralRe: good idea, but causes too much crashes Pin
PC-Alex8-Jun-06 22:48
memberPC-Alex8-Jun-06 22:48 
Generalcan't link to msdn 2005,but can link to platform sdk Pin
jacky_shenyu18-Jan-06 21:13
memberjacky_shenyu18-Jan-06 21:13 
GeneralRe: can't link to msdn 2005,but can link to platform sdk Pin
Chunhua Liu19-Apr-06 19:47
memberChunhua Liu19-Apr-06 19:47 
QuestionHow to take help of MSDN in VC++ 6 Pin
Anonymous8-Sep-05 18:46
sussAnonymous8-Sep-05 18:46 
General.dsw file open crash Pin
Daniel C.13-Jun-05 4:22
memberDaniel C.13-Jun-05 4:22 
GeneralGood, but a bug... Pin
liuhongxian9-May-05 18:54
memberliuhongxian9-May-05 18:54 
QuestionHow to bring up without F1 (with the Help menu) Pin
Vic Mackey16-Apr-05 10:39
memberVic Mackey16-Apr-05 10:39 
AnswerRe: How to bring up without F1 (with the Help menu) Pin
Chunhua Liu17-Apr-05 16:59
memberChunhua Liu17-Apr-05 16:59 
GeneralRe: How to bring up without F1 (with the Help menu) Pin
Vic Mackey17-Apr-05 17:58
memberVic Mackey17-Apr-05 17:58 
Generalvery good Pin
scircle15-Apr-05 21:30
memberscircle15-Apr-05 21:30 
Generalregistry key 0x0409 and sizeof error Pin
Harold Bamford10-Apr-05 7:43
memberHarold Bamford10-Apr-05 7:43 
GeneralRe: registry key 0x0409 and sizeof error Pin
Chunhua Liu10-Apr-05 9:30
memberChunhua Liu10-Apr-05 9:30 
GeneralRe: registry key 0x0409 and sizeof error Pin
Harold Bamford10-Apr-05 16:32
memberHarold Bamford10-Apr-05 16:32 
GeneralRe: registry key 0x0409 and sizeof error Pin
James R. Twine20-Apr-05 2:50
memberJames R. Twine20-Apr-05 2:50 
GeneralAddIn causes startup errors only if opening .dsw Pin
Harold Bamford10-Apr-05 7:28
memberHarold Bamford10-Apr-05 7:28 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
Chunhua Liu10-Apr-05 9:41
memberChunhua Liu10-Apr-05 9:41 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
Harold Bamford10-Apr-05 16:16
memberHarold Bamford10-Apr-05 16:16 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
Chunhua Liu10-Apr-05 19:47
memberChunhua Liu10-Apr-05 19:47 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
Lewis Lim11-May-05 20:32
memberLewis Lim11-May-05 20:32 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
DarkYoda M17-May-05 5:59
memberDarkYoda M17-May-05 5:59 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
Chunhua Liu17-May-05 10:28
memberChunhua Liu17-May-05 10:28 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
DarkYoda M17-May-05 21:08
memberDarkYoda M17-May-05 21:08 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
nvt15-Jun-05 20:43
membernvt15-Jun-05 20:43 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
DarkYoda M15-Jun-05 21:18
memberDarkYoda M15-Jun-05 21:18 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
nvt15-Jun-05 21:29
membernvt15-Jun-05 21:29 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
nvt15-Jun-05 21:43
membernvt15-Jun-05 21:43 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
DarkYoda M15-Jun-05 21:54
memberDarkYoda M15-Jun-05 21:54 
GeneralRe: AddIn causes startup errors only if opening .dsw Pin
Evan Langlois17-Oct-05 12:50
memberEvan Langlois17-Oct-05 12:50 
General6/5 ;) Pin
DarkYoda M7-Apr-05 22:45
memberDarkYoda M7-Apr-05 22:45 
GeneralThank you. Pin
krestanja5-Apr-05 7:34
memberkrestanja5-Apr-05 7:34 
GeneralVery helpful Pin
Vinaya28-Mar-05 23:13
memberVinaya28-Mar-05 23:13 
GeneralGood but... Pin
Sataron28-Mar-05 1:22
memberSataron28-Mar-05 1:22 
GeneralRe: Good but... Pin
Alberto Massari28-Mar-05 6:27
memberAlberto Massari28-Mar-05 6:27 
GeneralRe: Good but... Pin
DarkYoda M7-Apr-05 22:43
memberDarkYoda M7-Apr-05 22:43 
GeneralRe: Good but... Pin
Igor Solovyov10-Apr-05 1:47
memberIgor Solovyov10-Apr-05 1:47 
NewsRe: Good but... Pin
Pandele Florin27-Jun-06 23:17
memberPandele Florin27-Jun-06 23:17 
GeneralMS Add-ins recommended directory location Pin
Simon Said26-Mar-05 0:56
memberSimon Said26-Mar-05 0:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 28 Mar 2005
Article Copyright 2005 by Chunhua Liu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid