Win32 解压资源文件到内存
__inline static
LPBYTE TakeResource(LPBYTE* lpData, DWORD& dwSize, DWORD dwResID, LPCTSTR lpResType, HMODULE hModule = GetModuleHandle(NULL))
{
if (lpData == NULL)
{
return NULL;
}
HRSRC hRsrc = FindResource(hModule, MAKEINTRESOURCE(dwResID), lpResType);
if (hRsrc)
{
HGLOBAL hGlobal = LoadResource(hModule, hRsrc);
if (hGlobal)
{
LPVOID pData = LockResource(hGlobal);
if (pData)
{
if ((dwSize = SizeofResource(hModule, hRsrc)) > 0)
{
*lpData = (LPBYTE)malloc(dwSize * sizeof(BYTE));
CopyMemory(*lpData, pData, dwSize);
}
UnlockResource(hGlobal);
}
FreeResource(hRsrc);
}
}
return (*lpData);
}
__inline static
VOID FreeResource(LPBYTE* lpData)
{
if (lpData != NULL && (*lpData) != NULL)
{
free(*lpData);
}
}
__inline static
std::string TakeResourceData(DWORD dwResID, LPCTSTR lpResType, HMODULE hModule = GetModuleHandle(NULL))
{
std::string result = ("");
LPBYTE lpData = NULL;
DWORD dwSize = 0L;
TakeResource(&lpData, dwSize, dwResID, lpResType, hModule);
if (lpData != NULL)
{
result.assign((const char *)lpData, dwSize);
FreeResource(lpData);
}
return result;
}