1.得到当前exe的路径
string strPath;
char pchr[MAX_PATH] = {0};
GetModuleFileNameA(NULL, pchr, MAX_PATH);
strPath = pchr ;
strPath.append("\\..\\"); // .exe的上一级文件夹
strPath += "ResDll.dll";
#ifdef _DEBUG
::OutputDebugString(strPath.c_str());
#endif
if(!::PathFileExists(strPath.c_str())) // 判断文件是否存在
{
ReleaseRes(strPath.c_str(),(WORD)IDR_RESLIB_RESDLL,"ResLib");
if(!::PathFileExists(strPath.c_str()))
{
::AfxMessageBox("Release File Error, Please Close your anti-virus software!");
return FALSE;
}
}
2. 将资源释放到文件
BOOL ReleaseRes(LPCSTR strFileName,WORD wResID,CString strFileType)
{// 资源大小
DWORD dwWrite=0;
// 创建文件
HANDLE hFile = CreateFile(strFileName, GENERIC_WRITE,FILE_SHARE_WRITE,NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_TEMPORARY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM,NULL);
if ( hFile == INVALID_HANDLE_VALUE )
{
CString strMsg = "FzNetLicSvr: Release Res Fail ! --";
::OutputDebugString(strMsg + strFileName);
return FALSE;
}
// 查找资源文件中、加载资源到内存、得到资源大小
HRSRC hrsc = FindResource(NULL, MAKEINTRESOURCE(wResID), strFileType);
HGLOBAL hG = LoadResource(NULL, hrsc);
DWORD dwSize = SizeofResource( NULL, hrsc);
// 写入文件
WriteFile(hFile,hG,dwSize,&dwWrite,NULL);
CloseHandle( hFile );
return TRUE;
}