2012-10-30 44 views
4

我想實現一個COM的DllRegisterServer方法。
所以我閱讀本教程:
http://www.codeguru.com/cpp/com-tech/activex/tutorials/article.php/c5567/Step-by-Step-COM-Tutorial.htm實現DllRegisterServer方法

,我跟着,直到中的DllRegisterServer的一部分的步驟。
這是他們實現:

HRESULT __stdcall DllRegisterServer(void) 
    { 
    // 
    //As per COM guidelines, every self installable COM inprocess component 
    //should export the function DllRegisterServer for printing the 
    //specified information to the registry 
    // 
    // 

    WCHAR *lpwszClsid; 
    char szBuff[MAX_PATH]=""; 
    char szClsid[MAX_PATH]="", szInproc[MAX_PATH]="",szProgId[MAX_PATH]; 
    char szDescriptionVal[256]=""; 

    StringFromCLSID(
      CLSID_AddObject, 
      &lpwszClsid); 

    wsprintf(szClsid,"%S",lpwszClsid); 
    wsprintf(szInproc,"%s\\%s\\%s","clsid",szClsid,"InprocServer32"); 
    wsprintf(szProgId,"%s\\%s\\%s","clsid",szClsid,"ProgId"); 


    // 
    //write the default value 
    // 
    wsprintf(szBuff,"%s","Fast Addition Algorithm"); 
    wsprintf(szDescriptionVal,"%s\\%s","clsid",szClsid); 

    HelperWriteKey (
       HKEY_CLASSES_ROOT, 
       szDescriptionVal, 
       NULL,//write to the "default" value 
       REG_SZ, 
       (void*)szBuff, 
       lstrlen(szBuff) 
       ); 


    // 
    //write the "InprocServer32" key data 
    // 
    GetModuleFileName(
       g_hModule, 
       szBuff, 
       sizeof(szBuff)); 
    HelperWriteKey (
       HKEY_CLASSES_ROOT, 
       szInproc, 
       NULL,//write to the "default" value 
       REG_SZ, 
       (void*)szBuff, 
       lstrlen(szBuff) 
       ); 

    // 
    //write the "ProgId" key data under HKCR\clsid\{---}\ProgId 
    // 
    lstrcpy(szBuff,AddObjProgId); 
    HelperWriteKey (
       HKEY_CLASSES_ROOT, 
       szProgId, 
       NULL, 
       REG_SZ, 
       (void*)szBuff, 
       lstrlen(szBuff) 
       ); 


    // 
    //write the "ProgId" data under HKCR\CodeGuru.FastAddition 
    // 
    wsprintf(szBuff,"%s","Fast Addition Algorithm"); 
    HelperWriteKey (
       HKEY_CLASSES_ROOT, 
       AddObjProgId, 
       NULL, 
       REG_SZ, 
       (void*)szBuff, 
       lstrlen(szBuff) 
       ); 


    wsprintf(szProgId,"%s\\%s",AddObjProgId,"CLSID"); 
    HelperWriteKey (
       HKEY_CLASSES_ROOT, 
       szProgId, 
       NULL, 
       REG_SZ, 
       (void*)szClsid, 
       lstrlen(szClsid) 
       ); 

    return 1; 

    } 

其中CLSID_AddObject這樣定義:

// {92E7A9C2-F4CB-11d4-825D-00104B3646C0} 
static const GUID CLSID_AddObject = 
{ 0x92e7a9c2, 0xf4cb, 0x11d4, { 0x82, 0x5d, 0x0, 0x10, 0x4b, 0x36, 0x46, 0xc0 } }; 

我不明白的是: 1.爲什麼他們使用StringFromCLSID獲得的GUID字符串?他們已經擁有了它,並由於某種原因將它轉換爲IID?不是我們在IDL文件中給出的GUID足夠好嗎?
2.哪些GUID需要註冊?這個庫的GUID?接口的GUID?類GUID?或所有這些?

回答

1

之所以GUID轉換爲字符串的是,它的使用以在Windows註冊表中一些條目。您可以在示例代碼中看到CLSID字符串如何合併到InprocServer32,ProgIdCLSID條目中。

您應該在註冊表中註冊所有的GUID。有關COM註冊表項的詳細信息,請參閱this page in MSDN

+0

好的,但爲什麼他們使用StringFromCLSID,而不是僅僅使用 「{92E7A9C2-F4CB-11d4-825D-00104B3646C0}」? – Idov

+0

有一個專用的'GUID'類型(它不是一個字符串)。這種類型的值是爲您生成的,並在單個地方的頭文件中定義。你在哪裏看到相同的GUID值定義爲一個純字符串? – Stan

+0

在someting_h.h文件,它說:類DECLSPEC_UUID(「一些GUID」) – Idov