在vc6中按F1,可以使其跟MSDN2005关联起来

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"))

   HRESULT ALinkSearch (

       LPWSTR pszALink,

       long dwFlags,

       long dwReserved );

   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",

 

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 =

if (SUCCEEDED(hr))

{

   HRESULT hr =

   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.

 

Collapse//此处就是获取帮助文件的关联路径,//首先获取 HKEY_CURRENT_USER\\Software\\Microsoft\\Dependency Walker\\External Help,的//Collection值, 如果其为Online, 则使用URL中的地址,关联网上MSDN帮助//否则, 从//MSDN2003对应: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MSDN\\7.0\\Help\\0x0409//MSDN2005对应: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MSDN\\8.0\\Help\\0x0804//中, 查找名称为Collection的项, 关联其对应的FilenameHRESULT __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;//MSDN2003                                                          

//               lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,        

//                   "SOFTWARE\\Microsoft\\MSDN\\7.0\\Help\\0x0409",

//                   0, KEY_READ|KEY_ENUMERATE_SUB_KEYS, &hHelp);   //MSDN2005                                                                         lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,        

                   "SOFTWARE\\Microsoft\\MSDN\\8.0\\Help\\0x0804",

                   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?

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.

Run 'Depends.exe' under the SDK\bin directory.

Select menu 'Options'->'Configure External Function Help Collection'.

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!

 

 

 

 

 

posted @ 2011-11-29 23:07  Wait...  阅读(576)  评论(0编辑  收藏  举报