// DllCanUnloadNow() is called when COM wants to unload our DLL from memory.
// We check our lock count, which will be nonzero if there are any COM
// objects still in memory.
// Return S_FALSE to prevent the DLL from being unloaded, or S_OK to let it
// be unloaded.
STDAPI DllCanUnloadNow()
{
TRACE(">>> SimpleMsgBoxSvr: In DllCanUnloadNow(), ref count = %d\n", g_uDllLockCount );
return g_uDllLockCount > 0 ? S_FALSE : S_OK;
}
// 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;
}
#ifdef _DEBUG
//
// Diagnostic trace stolen shamelessly from MFC :)
#include <stdio.h>
void TRACE (LPCTSTR lpszFormat, ...)
{
va_list args;
int nBuf;
TCHAR szBuffer[512];
va_start(args, lpszFormat);
nBuf = _vsntprintf(szBuffer, 512, lpszFormat, args);
OutputDebugString ( szBuffer );
va_end(args);
}
#endif // ifdef _DEBUG
STDAPI DllRegisterServer()
最新推荐文章于 2022-04-27 10:51:29 发布