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; }
同意.
regsvr32 c:\mycom.dll
regsvr32 /u c:\mycom.dll
if(hLib == NULL)
{
AfxMessageBox("加载DLL失败!");
return;
}FARPROC lpDllEntryPoint;
lpDllEntryPoint = GetProcAddress(hLib, "DllRegisterServer");
if(lpDllEntryPoint != NULL)
{
if(FAILED((*lpDllEntryPoint)()))
{
AfxMessageBox("调用DllRegisterServer失败");
FreeLibrary(hLib);
return;
};
AfxMessageBox("注册成功");
}
else
AfxMessageBox("调用DllRegisterServer失败");
FreeLibrary(hLib);
STARTUPINFO StartupInfo;
ZeroMemory(&StartupInfo,sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo; _tcscat(path, _T("\\Test.dll\" /s"));//path:Test.dll的目录,目录有空格要加引号“C:\Program Files\Test.dll”
TCHAR szPath[MAX_PATH]={0};
_tcscpy(szPath,_T("regsvr32.exe \""));
_tcscat(szPath,path);
if(CreateProcess(NULL,szPath,NULL,NULL,FALSE,0,NULL,NULL,&StartupInfo,&ProcessInfo) )
{ //注册成功的话,等待注册完成
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
}
{
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;
}