VC ++ 注册DLL
来源:互联网 发布:前端页面性能优化方法 编辑:程序博客网 时间:2024/06/05 18:44
文章出处:http://blog.csdn.net/cddchina/article/details/19490625
VC++注册DLL
1.注册dll
- #include <Shlwapi.h>
- #pragma comment(lib, "Shlwapi.lib")
-
-
-
-
- BOOL RegistryDll(CString& szDllPath)
- {
- if (!(PathFileExists(szDllPath) && (!PathIsDirectory(szDllPath))))
- {
- wprintf(L"注册%s 文件的时候,发生错误:该文件不存在!\r\n", szDllPath);
-
- return FALSE;
- }
- LRESULT (CALLBACK* lpDllEntryPoint)();
- HINSTANCE hLib = LoadLibrary(szDllPath);
- if (hLib < (HINSTANCE)HINSTANCE_ERROR)
- return FALSE;
- (FARPROC&)lpDllEntryPoint = GetProcAddress(hLib, "DllRegisterServer");
- BOOL bRet = FALSE;
- if (lpDllEntryPoint != NULL)
- {
- HRESULT hr = (*lpDllEntryPoint)();
- bRet = SUCCEEDED(hr);
- if (FAILED(hr))
- {
- CString szErrMsg;
- if (GetErrMessage(hr, szErrMsg))
- {
- wprintf(L"注册%s 文件的时候,发生错误:%s\r\n", szDllPath, szErrMsg);
- }
- else
- {
- wprintf(L"注册%s 文件的时候,发生错误,但是无法获取错误信息,错误代码为:x%08x\r\n", szDllPath, hr);
- }
- }
- }
- FreeLibrary(hLib);
- return bRet;
- }
2.反注册dll
-
- BOOL UnRegistryDll(CString& szDllPath)
- {
- if (!(PathFileExists(szDllPath) && (!PathIsDirectory(szDllPath))))
- {
- wprintf(L"反注册%s 文件的时候,发生错误:该文件不存在!\r\n", szDllPath);
- return FALSE;
- }
- LRESULT (CALLBACK* lpDllEntryPoint)();
- HINSTANCE hLib = LoadLibrary(szDllPath);
- if (hLib < (HINSTANCE)HINSTANCE_ERROR)
- return FALSE;
- (FARPROC&)lpDllEntryPoint = GetProcAddress(hLib, "DllUnregisterServer");
- BOOL bRet = FALSE;
- if (lpDllEntryPoint != NULL)
- {
- HRESULT hr = (*lpDllEntryPoint)();
- bRet = SUCCEEDED(hr);
- if (FAILED(hr))
- {
- CString szErrMsg;
- if (GetErrMessage(hr, szErrMsg))
- {
- wprintf(L"反注册%s 文件的时候,发生错误:%s\r\n", szDllPath, szErrMsg);
- }
- else
- {
- wprintf(L"反注册%s 文件的时候,发生错误,但是无法获取错误信息,错误代码为:x%08x\r\n", szDllPath, hr);
- }
- }
- }
- FreeLibrary(hLib);
- return bRet;
- }
摘自--
0 0