5

我正在尝试实现 COM 的 DllRegisterServer 方法。
所以我读了这个教程: http:
//www.codeguru.com/cpp/com-tech/activex/tutorials/article.php/c5567/Step-by-Step-COM-Tutorial.htm

我按照这些步骤直到 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?还是全部?

4

1 回答 1

1

GUID 转换为字符串的原因是它用于在 Windows 注册表中形成一些条目。您可能会在示例代码中看到 CLSID 字符串如何合并到InprocServer32ProgIdCLSID条目中。

您应该在注册表中注册所有 GUID。您可以查看MSDN 中的此页面,了解有关 COM 注册表项的详细信息。

于 2012-10-30T09:59:03.597 回答