// DllRegisterServer() creates the registy entries that tells COM where our
// server is located and its threading model.
STDAPI DllRegisterServer()
{
HKEY hCLSIDKey = NULL, hInProcSvrKey = NULL;
LONG lRet;
TCHAR szModulePath [MAX_PATH];
TCHAR szClassDescription[] = _T("SimpleMsgBox class");
TCHAR szThreadingModel[] = _T("Apartment");
__try
{
// Create a key under CLSID for our COM server.
lRet = RegCreateKeyEx ( HKEY_CLASSES_ROOT, _T("CLSID\\{7D51904E-1645-4a8c-BDE0-0F4A44FC38C4}"),
0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE | KEY_CREATE_SUB_KEY,
NULL, &hCLSIDKey, NULL );
if ( ERROR_SUCCESS != lRet )
return HRESULT_FROM_WIN32(lRet);
// The default value of the key is a human-readable description of the coclass.
lRet = RegSetValueEx ( hCLSIDKey, NULL, 0, REG_SZ, (const BYTE*) szClassDescription,
sizeof(szClassDescription) );
if ( ERROR_SUCCESS != lRet )
return HRESULT_FROM_WIN32(lRet);
// Create the InProcServer32 key, which holds info about our coclass.
lRet = RegCreateKeyEx ( hCLSIDKey, _T("InProcServer32"), 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_SET_VALUE, NULL, &hInProcSvrKey, NULL );
if ( ERROR_SUCCESS != lRet )
return HRESULT_FROM_WIN32(lRet);
// The default value of the InProcServer32 key holds the full path to our DLL.
GetModuleFileName ( g_hinstThisDll, szModulePath, MAX_PATH );
lRet = RegSetValueEx ( hInProcSvrKey, NULL, 0, REG_SZ, (const BYTE*) szModulePath,
sizeof(TCHAR) * (lstrlen(szModulePath)+1) );
if ( ERROR_SUCCESS != lRet )
return HRESULT_FROM_WIN32(lRet);
// The ThreadingModel value tells COM how it should handle threads in our DLL.
// The concept of apartments is beyond the scope of this article, but for
// simple, single-threaded DLLs, use Apartment.
lRet = RegSetValueEx ( hInProcSvrKey, _T("ThreadingModel"), 0, REG_SZ,
(const BYTE*) szThreadingModel,
sizeof(szThreadingModel) );
if ( ERROR_SUCCESS != lRet )
return HRESULT_FROM_WIN32(lRet);
}
__finally
{
if ( NULL != hCLSIDKey )
RegCloseKey ( hCLSIDKey );
if ( NULL != hInProcSvrKey )
RegCloseKey ( hInProcSvrKey );
}
return S_OK;
}
// DllUnregisterServer() deleted the registy entries that DllRegisterServer() created.
STDAPI DllUnregisterServer()
{
// Delete our registry entries. Note that you must delete from the deepest
// key and work upwards, because on NT/2K, RegDeleteKey() doesn't delete
// keys that have subkeys on NT/2K.
RegDeleteKey ( HKEY_CLASSES_ROOT, _T("CLSID\\{7D51904E-1645-4a8c-BDE0-0F4A44FC38C4}\\InProcServer32") );
RegDeleteKey ( HKEY_CLASSES_ROOT, _T("CLSID\\{7D51904E-1645-4a8c-BDE0-0F4A44FC38C4}") );
return S_OK;
}
DllRegisterServer()
最新推荐文章于 2023-12-26 12:57:57 发布