1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
// Topic : Extract an executable resource to the disk.
// [IN] [tstring] Path : Path to file where resource data are written.
// [IN] [DWORD] ResId : ID of the resource.
// Return [bool] : true on success, false otherwise.
bool ExtractResource(const tstring& Path, DWORD ResId)
{
// find resource with appropriate ID.
HRSRC hMod = ::FindResource(NULL, MAKEINTRESOURCE(ResId), _T("BIN"));
if(hMod == NULL)
return false;
// get size of res.
DWORD ResSize = ::SizeofResource(NULL, hMod);
if(ResSize == 0)
return false;
// Load the res.
HGLOBAL hGlob = ::LoadResource(NULL, hMod);
if(hGlob == NULL)
return false;
// obtain a pointer on resource data.
LPVOID pData = ::LockResource(hGlob);
if (pData == NULL)
return false;
// Create a file for writing resource data to disk.
HANDLE hFile = ::CreateFile(Path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL);
if(hFile == INVALID_HANDLE_VALUE)
return false;
// Write the data resource to file.
DWORD nobw = 0;
if(::WriteFile(hFile, pData, ResSize, &nobw, NULL) == FALSE)
{
CloseHandle(hFile);
return false;
}
// release file object.
CloseHandle(hFile);
return true;
} |
Partager