[C++] WinApi: How to Extract Resource To File
void ExtractBinResource(TCHAR* strCustomResName,
int nResourceId,
TCHAR* strOutputName)
{
HGLOBAL hResourceLoaded; // handle to loaded resource
HRSRC hRes; // handle/ptr to res. info.
char *lpResLock; // pointer to resource data
DWORD dwSizeRes;
// lets get the app location
hRes = FindResource(GetCurrentModuleHandle(),
MAKEINTRESOURCE(nResourceId),
strCustomResName
);
hResourceLoaded = LoadResource(GetCurrentModuleHandle(), hRes);
lpResLock = (char *)LockResource(hResourceLoaded);
dwSizeRes = SizeofResource(GetCurrentModuleHandle(), hRes);
std::ofstream outputFile(strOutputName, std::ios::binary);
outputFile.write((const char *)lpResLock, dwSizeRes);
outputFile.close();
UnlockResource(hResourceLoaded);
}
HMODULE GetCurrentModuleHandle()
{
HMODULE hMod = NULL;
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(&GetCurrentModuleHandle), &hMod);
return hMod;
}