mainfest资源,在这里指的是资源类型为RT_MANIFEST的资源信息。通过ResHacker看到的效果图如下:
manifest资源存储编码格式是UTF-8(开始3个字节是EF BB BF),解析代码如下:
// UTF8-EF BB BF
HRSRC hResrc = ::FindResourceEx((HMODULE)hModule, lpType, lpName, wLanguage);
DWORD dwSize = ::SizeofResource((HMODULE)hModule, hResrc);
HGLOBAL hGlobal = ::LoadResource((HMODULE)hModule, hResrc);
if (hGlobal == NULL)
return;
BYTE* lpResrc = (BYTE*)LockResource(hGlobal);
if (lpResrc == NULL)
return;
HGLOBAL hAllocMem = GlobalAlloc(GMEM_FIXED, dwSize);
char* pMem = (char*)GlobalLock(hAllocMem);
memcpy(pMem, lpResrc, dwSize);
short length = dwSize;
char* pBuffer = new char[length + 1];
memset(pBuffer, 0, length + 1);
memcpy((char*)pBuffer, (char*)pMem, length);
std::string sResult = Utf8ToGB2312(&pBuffer[3]);
delete[] pBuffer;
GlobalUnlock(hAllocMem);
GlobalFree(hAllocMem);
//sResult就是manifest资源的内容
这里用到了一个UTF8字符串转换函数,代码如下:
//UTF-8到GB2312的转换
std::string Utf8ToGB2312(const char* utf8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len];
memset(wstr, 0, len);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char* gb2312 = new char[len];
memset(gb2312, 0, len);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, gb2312, len, NULL, NULL);
std::string result = std::string(gb2312);
if (wstr) delete[] wstr;
if (gb2312) delete[] gb2312;
return result;
}