姑且算是有C和C++两个版本吧。
//C
BOOL GetResource(HMODULE mod, const char* rname, const char* rtype, unsigned char** rdata, int* rlen)
{
HRSRC rhandle = FindResourceA(mod, rname, rtype);
if (rhandle == NULL)
return FALSE;
*rlen = SizeofResource(mod, rhandle);
HGLOBAL global_handle = LoadResource(mod, rhandle);
if (NULL == global_handle)
return FALSE;
*rdata = LockResource(global_handle);
if (*rdata)
{
DWORD flOldProtect;
VirtualProtect(*rdata, *rlen, PAGE_READWRITE, &flOldProtect);
return TRUE;
}
FreeResource(global_handle);
return FALSE;
}
//C++
bool GetResource(HMODULE module, const char * rname, const char * rtype, byte* &rdata, int &rlen)
{
HRSRC rhandle = FindResourceA(module, rname, rtype);
if (rhandle == NULL)
return false;
rlen = SizeofResource(module, rhandle);
HGLOBAL global_handle = LoadResource(module, rhandle);
if (NULL == global_handle)
return false;
rdata = reinterpret_cast<byte*>(LockResource(global_handle));
if (rdata)
{
DWORD flOldProtect;
VirtualProtect(rdata, rlen, PAGE_READWRITE, &flOldProtect);
return true;
}
FreeResource(global_handle);
return false;
}
发表回复