VS2008 將資源釋放到文件

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;
}
發佈了2 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章