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;
}