C++ COM注册这个DLL

DLL做好之后就可以实现注册代码了,注册之后客户端就可以通过注册表查询到该DLL并进行调用。下面是注册过程,其中使用的两个文件如下:

 

[c-sharp]  view plain copy
  1. //-----------------------------------------------------------------------------  
  2. // File: registry.h  
  3. //  
  4. // Desc: Contains COM register and unregister functions for the UI.  
  5. //  
  6. // Copyright (C) 1999-2001 Microsoft Corporation. All Rights Reserved.  
  7. //-----------------------------------------------------------------------------  
  8. #ifndef __Registry_H__  
  9. #define __Registry_H__  
  10. // This function will register a component in the Registry.  
  11. // The component calls this function from its DllRegisterServer function.  
  12. HRESULT RegisterServer(HMODULE hModule,   
  13.     const CLSID& clsid,   
  14.     LPCTSTR szFriendlyName,  
  15.     LPCTSTR szVerIndProgID,  
  16.     LPCTSTR szProgID);  
  17. // This function will unregister a component.  Components  
  18. // call this function from their DllUnregisterServer function.  
  19. HRESULT UnregisterServer(const CLSID& clsid,  
  20.     LPCTSTR szVerIndProgID,  
  21.     LPCTSTR szProgID);  
  22. #endif  
 

[c-sharp]  view plain copy
  1. //  
  2. // Registry.cpp  
  3. //  
  4. #include "stdafx.h"  
  5. #include <objbase.h>  
  6. #include <assert.h>  
  7. #include "registry.h"  
  8. #pragma warning(disable:4996)  
  9.   
  10. //  
  11. // Internal helper functions prototypes  
  12. //  
  13. // Set the given key and its value.  
  14. BOOL setKeyAndValue(const char* pszPath,  
  15.     const char* szSubkey,  
  16.     const char* szValue);  
  17. // Convert a CLSID into a char string.  
  18. void CLSIDtochar(const CLSID& clsid,   
  19.     char* szCLSID,  
  20.     int length);  
  21. // Delete szKeyChild and all of its descendents.  
  22. LONG recursiveDeleteKey(HKEY hKeyParent, const char* szKeyChild);  
  23.   
  24. //  
  25. // Constants  
  26. //  
  27. // Size of a CLSID as a string  
  28. const int CLSID_STRING_SIZE = 39;  
  29. /  
  30. //  
  31. // Public function implementation  
  32. //  
  33. //  
  34. // Register the component in the registry.  
  35. //  
  36. HRESULT RegisterServer(HMODULE hModule,            // DLL module handle  
  37.     const CLSID& clsid,         // Class ID  
  38.     const char* szFriendlyName, // Friendly Name  
  39.     const char* szVerIndProgID, // Programmatic  
  40.     const char* szProgID)       //   IDs  
  41. {  
  42.     // Get server location.  
  43.     char szModule[512];  
  44.     DWORD dwResult = ::GetModuleFileName(hModule, szModule, sizeof(szModule)/sizeof(char));  
  45.     assert(dwResult != 0);  
  46.     // Convert the CLSID into a char.  
  47.     char szCLSID[CLSID_STRING_SIZE];  
  48.     CLSIDtochar(clsid, szCLSID, sizeof(szCLSID));  
  49.     // Build the key CLSID//{...}  
  50.     char szKey[64];  
  51.     strcpy(szKey, "CLSID//");  
  52.     strcat(szKey, szCLSID);  
  53.     // Add the CLSID to the registry.  
  54.     setKeyAndValue(szKey, NULL, szFriendlyName);  
  55.     // Add the server filename subkey under the CLSID key.  
  56.     setKeyAndValue(szKey, "InprocServer32", szModule);  
  57.     // Add the ProgID subkey under the CLSID key.  
  58.     setKeyAndValue(szKey, "ProgID", szProgID);  
  59.     // Add the version-independent ProgID subkey under CLSID key.  
  60.     setKeyAndValue(szKey, "VersionIndependentProgID", szVerIndProgID);  
  61.     // Add the version-independent ProgID subkey under HKEY_CLASSES_ROOT.  
  62.     setKeyAndValue(szVerIndProgID, NULL, szFriendlyName);   
  63.     setKeyAndValue(szVerIndProgID, "CLSID", szCLSID);  
  64.     setKeyAndValue(szVerIndProgID, "CurVer", szProgID);  
  65.     // Add the versioned ProgID subkey under HKEY_CLASSES_ROOT.  
  66.     setKeyAndValue(szProgID, NULL, szFriendlyName);   
  67.     setKeyAndValue(szProgID, "CLSID", szCLSID);  
  68.     return S_OK;  
  69. }  
  70. //  
  71. // Remove the component from the registry.  
  72. //  
  73. LONG UnregisterServer(const CLSID& clsid,         // Class ID  
  74.     const char* szVerIndProgID, // Programmatic  
  75.     const char* szProgID)       //   IDs  
  76. {  
  77.     // Convert the CLSID into a char.  
  78.     char szCLSID[CLSID_STRING_SIZE];  
  79.     CLSIDtochar(clsid, szCLSID, sizeof(szCLSID));  
  80.     // Build the key CLSID//{...}  
  81.     char szKey[64];  
  82.     strcpy(szKey, "CLSID//");  
  83.     strcat(szKey, szCLSID);  
  84.     // Delete the CLSID Key - CLSID/{...}  
  85.     LONG lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szKey);  
  86.     assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)); // Subkey may not exist.  
  87.     // Delete the version-independent ProgID Key.  
  88.     lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szVerIndProgID);  
  89.     assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)); // Subkey may not exist.  
  90.     // Delete the ProgID key.  
  91.     lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szProgID);  
  92.     assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)); // Subkey may not exist.  
  93.     return S_OK;  
  94. }  
  95. ///  
  96. //  
  97. // Internal helper functions  
  98. //  
  99. // Convert a CLSID to a char string.  
  100. void CLSIDtochar(const CLSID& clsid,  
  101.     char* szCLSID,  
  102.     int length)  
  103. {  
  104.     assert(length >= CLSID_STRING_SIZE);  
  105.     // Get CLSID  
  106.     LPOLESTR wszCLSID = NULL;  
  107.     HRESULT hr = StringFromCLSID(clsid, &wszCLSID);  
  108.     assert(SUCCEEDED(hr));  
  109.     // Covert from wide characters to non-wide.  
  110.     wcstombs(szCLSID, wszCLSID, length);  
  111.     // Free memory.  
  112.     CoTaskMemFree(wszCLSID);  
  113. }  
  114. //  
  115. // Delete a key and all of its descendents.  
  116. //  
  117. LONG recursiveDeleteKey(HKEY hKeyParent,           // Parent of key to delete  
  118.     const char* lpszKeyChild)  // Key to delete  
  119. {  
  120.     // Open the child.  
  121.     HKEY hKeyChild;  
  122.     LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyChild, 0, KEY_ALL_ACCESS, &hKeyChild);  
  123.     if(lRes != ERROR_SUCCESS)  
  124.     {  
  125.         return lRes;  
  126.     }  
  127.     // Enumerate all of the decendents of this child.  
  128.     FILETIME time;  
  129.     char szBuffer[256];  
  130.     DWORD dwSize = 256;  
  131.     while(RegEnumKeyEx(hKeyChild, 0, szBuffer, &dwSize, NULL, NULL, NULL, &time) == S_OK)  
  132.     {  
  133.         // Delete the decendents of this child.  
  134.         lRes = recursiveDeleteKey(hKeyChild, szBuffer);  
  135.         if(lRes != ERROR_SUCCESS)  
  136.         {  
  137.             // Cleanup before exiting.  
  138.             RegCloseKey(hKeyChild);  
  139.             return lRes;  
  140.         }  
  141.         dwSize = 256;  
  142.     }  
  143.     // Close the child.  
  144.     RegCloseKey(hKeyChild);  
  145.     // Delete this child.  
  146.     return RegDeleteKey(hKeyParent, lpszKeyChild);  
  147. }  
  148. //  
  149. // Create a key and set its value.  
  150. //   - This helper function was borrowed and modifed from  
  151. //     Kraig Brockschmidt's book Inside OLE.  
  152. //  
  153. BOOL setKeyAndValue(const char* szKey,  
  154.     const char* szSubkey,  
  155.     const char* szValue)  
  156. {  
  157.     HKEY hKey;  
  158.     char szKeyBuf[1024];  
  159.     // Copy keyname into buffer.  
  160.     strcpy(szKeyBuf, szKey);  
  161.     // Add subkey name to buffer.  
  162.     if(szSubkey != NULL)  
  163.     {  
  164.         strcat(szKeyBuf, "//");  
  165.         strcat(szKeyBuf, szSubkey );  
  166.     }  
  167.     // Create and open key and subkey.  
  168.     long lResult = RegCreateKeyEx(HKEY_CLASSES_ROOT, szKeyBuf, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);  
  169.     if(lResult != ERROR_SUCCESS)  
  170.     {  
  171.         return FALSE;  
  172.     }  
  173.     // Set the Value.  
  174.     if(szValue != NULL)  
  175.     {  
  176.         RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE *)szValue, (DWORD)strlen(szValue)+1);  
  177.     }  
  178.     RegCloseKey(hKey);  
  179.     return TRUE;  
  180. }  
 

 

registry.h和Registry.cpp两个文件实现对一个进程内COM组件的注册。

 

现对注册函数修改如下:

[c-sharp]  view plain copy
  1. STDAPI DllUnregisterServer(void)  
  2. {  
  3.     return UnregisterServer(CLSID_MyCOM, VerIndProgID, ProgID);  
  4. }  
  5. STDAPI DllRegisterServer(void)  
  6. {  
  7.     return RegisterServer(g_hInstance, CLSID_MyCOM, FriendlyName, VerIndProgID, ProgID);  
  8. }  
 

 

经过上述步骤DLL的注册已经实现,可以使用regsvr32 mycom.dll进行注册了。

其中CLSID_MyCOM, FriendlyName, VerIndProgID, ProgID等定义如下:

 

[c-sharp]  view plain copy
  1. // {85F5CF11-2A25-4ef9-8F7C-8C3F14A0860A}  
  2. static const GUID CLSID_MyCOM =   
  3. { 0x85f5cf11, 0x2a25, 0x4ef9, { 0x8f, 0x7c, 0x8c, 0x3f, 0x14, 0xa0, 0x86, 0xa } };  
  4.  
  5. #define ProgID          "MyCOM.CTest.1"  
  6. #define VerIndProgID    "MyCOM.CTest"  
  7. #define FriendlyName    "MyCOM CTest Sample Provider"  
 

CLSID_MyCOM :组件ID

ProgID:程序ID

VerIndProgID:版本无关的程序ID

FriendlyName:组件描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值