我正在尝试实现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需要注册? Ther库的GUID?接口的GUID?班级GUID?还是所有人?
答案 0 :(得分:1)
将GUID转换为字符串的原因是它用于在Windows注册表中形成一些条目。您可以在示例代码中看到CLSID字符串如何合并到InprocServer32
,ProgId
和CLSID
条目中。
您应该在注册表中注册所有GUID。有关COM注册表项的详细信息,请查看this page in MSDN。