Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C++] Loading resources

Started by ta0soft Mar 29, 2011 at 3:34 PM 2 replies 1.7k views
Original Post
ta0soft
ta0soft
Hello, my program contains some text files as resources in binary format (RT_RCDATA) and I'm trying to load them at runtime.

From what I understand, LockResource returns a pointer to the first byte of the resource, but how do I read all of the bytes and store them in a string or char array? I can't find any good examples on the net


HMODULE module = GetModuleHandle(NULL);
HRSRC resInfo = FindResource(module, MAKEINTRESOURCE(resourceID), RT_RCDATA);
DWORD resSize = SizeofResource(module, resInfo);
HGLOBAL resHandle = LoadResource(module, resInfo);
char *resData = (char*)LockResource(resHandle);
// ...?
Buckeye
Buckeye
memcpy? strcpy?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
kdmiller3
kdmiller3
LockResource() returns a pointer to the entire resource, not just a single byte, so you can treat it like a struct or array and memcpy it to your buffer. You got its size with SizeofResource() so all you need is a destination buffer of the appropriate size.
ta0soft
ta0soft
Well, my app is unicode so I think I need to use wcscpy instead of strcopy. It seems to be working but the string isn't encoded correctly. It just returns a bunch of asian characters

here is my full code:

bool Load_ResourceW(DWORD resourceID)
{
HMODULE module = GetModuleHandle(NULL);
HRSRC resInfo = FindResource(module, MAKEINTRESOURCE(resourceID), RT_RCDATA);
if (!resInfo) return false;

DWORD resSize = SizeofResource(module, resInfo);
if (resSize == 0) return false;

HGLOBAL resHandle = LoadResource(module, resInfo);
wchar_t* resData = (wchar_t*)LockResource(resHandle);
wchar_t *resData2 = new wchar_t[resSize];
wcscpy(resData2, resData);

MessageBox(NULL, resData2, NULL, MB_OK);
return true;
};

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.