一個(gè)COM組件在使用前必須首先注冊(cè)。所謂“注冊(cè)”,也就是向系統(tǒng)注冊(cè)表的相應(yīng)位置寫入一些數(shù)據(jù)。這些數(shù)據(jù)可以完成guid與Dll的絕對(duì)路徑的一一對(duì)應(yīng),也就是說可以幫助程序通過guid找到Dll的位置。
GUID概念:
GUID(globally unique identifier)是一個(gè)128位的數(shù)。用于保證每一個(gè)接口和組件在時(shí)間和空間上都是全球唯一的一個(gè)標(biāo)識(shí)符。為保證空間唯一性,根據(jù)機(jī)器上網(wǎng)卡的MAC地址再加上一定的算法生成的唯一的48位值序列;為保證時(shí)間上的唯一性,每個(gè)GUID值具有一個(gè)60位的時(shí)間戳。這個(gè)時(shí)間戳表示的是自1852年10月15號(hào)00:00:00以來以100納秒為時(shí)間間隔的計(jì)數(shù)值。這樣可以保證3400年GUID值仍然是唯一的。下面是GUID的定義和一個(gè)示例:
typedef struct _GUID {
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
} GUID;
// {364ede61-08ac-43ec-8861-15f5f9f4ced1}
微軟提供了兩個(gè)建立GUID的程序,一個(gè)時(shí)UUIDGEN.EXE,該程序是命令行方式的;另一個(gè)則是GUIDGEN.EXE,是一個(gè)示例性的VC++對(duì)話框應(yīng)用。
DEFINE_GUID可以使用GUIDGEN.EXE來生成一個(gè)GUID。
注冊(cè)表:
組件可以用CLSID作為索引在Windows的注冊(cè)表中發(fā)布包含他們的DLL文件名稱。CoCreateInstance將用CLSID作為關(guān)鍵字在注冊(cè)表中查找所需要的文件名稱。注
冊(cè)表是一個(gè)由許多元素構(gòu)成的層次結(jié)構(gòu)。每一個(gè)元素均被稱作是一個(gè)關(guān)鍵字。每一個(gè)關(guān)鍵字可以包含一系列子關(guān)鍵字、一系列命名的值及/或一個(gè)未命名的值。COM只
使用了注冊(cè)表的一個(gè)分支:HKEY_CLASSES_ROOT;在此關(guān)鍵字之下,可以看到有一個(gè)CLSID關(guān)鍵字。在CLSID關(guān)鍵字之下列有系統(tǒng)中安裝的所有組件的CLSID。對(duì)于
每一個(gè)CLSID關(guān)鍵字,我們現(xiàn)在關(guān)心的只是它的一個(gè)子關(guān)鍵字InprocServer32。此子關(guān)鍵字的缺省值是組件所在的Dll文件路徑名稱。另一個(gè)子關(guān)鍵字ProgID指的是程序
員給某個(gè)CLSID指定的一個(gè)程序員易記的名稱。
如何注冊(cè)COM組件:
由于DLL知道它所包含的組件,因此DLL可以完成這些信息的注冊(cè)。在DLL中一定要處處如下兩個(gè)函數(shù):
DllRegisterServer();// 完成注冊(cè)組件
DllUnRegisterServer();// 完成反注冊(cè)組件
用戶可以使用程序REGSVR32.EXE來注冊(cè)某個(gè)組件。方法是使用命令行:regsvr32/u testDll.dll(反注冊(cè)) regsvr32 testDll.dll(注冊(cè))。
一個(gè)典型的注冊(cè)COM組件Dll必須導(dǎo)出如下五個(gè)函數(shù):
1)DllMain:Dll的入口函數(shù),完成一些Dll的初始化工作(DirectShow實(shí)現(xiàn)的是DllEntryPoint);
2)DllGetClassObject:用于獲得類工廠指針;
3)DllCanUnloadNow: 系統(tǒng)空閑時(shí)會(huì)調(diào)用這個(gè)函數(shù),以確定是否可以卸載Dll;
4)DllRegisterServer:將COM組件注冊(cè)到注冊(cè)表中;
5)DllUnregisterServer: 刪除注冊(cè)表中COM組件的注冊(cè)信息。
所以,要想完成注冊(cè),關(guān)鍵就是對(duì)DllRegisterServer();DllUnRegisterServer();兩個(gè)函數(shù)的實(shí)現(xiàn),下面將詳細(xì)介紹如何實(shí)現(xiàn)這兩個(gè)函數(shù):
//g_module為DLL的實(shí)例句柄
//CLSID_INNER_COM為組件的CLSID
//const char g_friend_name_inner [] = "inner_com_test";
//const char g_ver_indprog_id_inner [] = "inside.com.chapter.7.inner";
//const char g_prog_id_inner [] = "inside.com.chapter.7.inner.1";
STDAPI DllRegisterServer(void)
{
HRESULT hr = RegisterServer(g_module,CLSID_INNER_COM,
g_friend_name_inner,g_ver_indprog_id_inner,g_prog_id_inner);
return hr;
}
//Register the component in the registry
HRESULT RegisterServer(HMODULE hModule,
const CLSID& clsid,
const char* szFriendlyName,
const char* szVerIndProgID,
const char* szProgID)
{
//Get the Server location
char szModule[512];
DWORD dwResult = ::GetModuleFileName(hModule,szModule,sizeof(szModule)/sizeof(char));
assert(dwResult!=0);
//Convert the CLSID into a char
char szCLSID[CLSID_STRING_SIZE];
CLSIDtochar(clsid,szCLSID,sizeof(szCLSID));
//Build the key CLSID\\{}
char szKey[64];
strcpy(szKey,"CLSID\\");
strcat(szKey,szCLSID);
//Add the CLSID to the registry
setKeyAndValue(szKey,NULL,szFriendlyName);
//Add the Server filename subkey under the CLSID key
setKeyAndValue(szKey,"InprocServer32",szModule);
setKeyAndValue(szKey,"ProgID",szProgID);
setKeyAndValue(szKey,"VersionIndependentProgID",szVerIndProgID);
//Add the version-independent ProgID subkey under HKEY_CLASSES_ROOT
setKeyAndValue(szVerIndProgID,NULL,szFriendlyName);
setKeyAndValue(szVerIndProgID,"CLSID",szCLSID);
setKeyAndValue(szVerIndProgID,"CurVer",szProgID);
//Add the versioned ProgID subkey under HKEY_CLASSES_ROOT
setKeyAndValue(szProgID,NULL,szFriendlyName);
setKeyAndValue(szProgID,"CLSID",szCLSID);
return S_OK;
}
STDAPI DllUnregisterServer(void)
{
HRESULT hr = UnRegisterServer(CLSID_INNER_COM,g_ver_indprog_id_inner,g_prog_id_inner);
return hr;
}
//Remove the component from the register
HRESULT UnRegisterServer(const CLSID& clsid, // Class ID
const char* szVerIndProgID, // Programmatic
const char* szProgID) // IDs
{
//Convert the CLSID into a char.
char szCLSID[CLSID_STRING_SIZE];
CLSIDtochar(clsid,szCLSID,sizeof(szCLSID));
//Build the key CLSID\\{}
char szKey[64];
strcpy(szKey,"CLSID\\");
strcat(szKey,szCLSID);
//Delete the CLSID key - CLSID\{}
LONG lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT,szKey);
assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND));
//Delete the version-independent ProgID Key
lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT,szVerIndProgID);
assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND));
//Delete the ProgID key.
lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT,szProgID);
assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND));
return S_OK;
}