1. 查找资源并获取资源内容
/**
* exportPath:文件路径,
*resourceId:资源ID
*/
bool ExportRes(const std::wstring& exportPath, DWORD resourceId)
{
// "DLL" 是自定义资源类型,可以自己决定
HRSRC hrSrc = FindResource(m_hInstance, MAKEINTRESOURCE(resourceId), _T("DLL"));
if (hrSrc == NULL)
{
return false;
}
HGLOBAL hGlobalResource = LoadResource(m_hInstance, hrSrc);
if (hGlobalResource == NULL)
{
return false;
}
const void* pResourceData = ::LockResource(hGlobalResource);
if (!pResourceData)
{
return false;
}
DWORD resLength = SizeofResource(m_hInstance, hrSrc);
bool ret = ExportToFile(exportPath, pResourceData, resLength);
FreeResource(hGlobalResource);
return ret;
}
bool ExportToFile(const std::wstring& exportFilePath, const void* pBuffer, DWORD bufferLength)
{
if (pBuffer == NULL || bufferLength <= 0)
{
return false;
}
HANDLE hFile = ::CreateFile(exportFilePath.c_str(),
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == NULL)
{
return false;
}
DWORD writetem = -1;
BOOL ret = ::WriteFile(hFile, pBuffer, bufferLength, &writetem, NULL);
if (writetem != bufferLength)
{
::CloseHandle(hFile);
return false;
}
::CloseHandle(hFile);
return true;
}