Skip to main content
gamedev.net
🔒 Locked

Loading binary resources

Started by Exellion 2 replies 3.5k views
Original Post
Exellion
Exellion
Hi, and here we go again: I''ve encountered a disturbing problem when trying to load a resource with MSVC .NET... I spent the last half hour experimenting with different parameter combinations, file types and solutions, but I still don''t understand what''s wrong, so lets take a look at the simple program:
#include <windows.h>
#include "resource.h"

INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
	HRSRC bitmap = FindResource(NULL, MAKEINTRESOURCE(IDB_BITMAP1), RT_BITMAP);
	HRSRC bitmapEx = FindResourceEx(NULL, MAKEINTRESOURCE(IDB_BITMAP1), RT_BITMAP, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));

	HRSRC binary = FindResource(NULL, MAKEINTRESOURCE(IDR_KEK1), RT_RCDATA);
	HRSRC binaryEx = FindResourceEx(NULL, MAKEINTRESOURCE(IDR_KEK1), RT_RCDATA, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));

	return (0);
}
Resource.h cointains two defines (IDB_BITMAP1 and IDR_KEK1). IDB_BITMAP1 is a bitmap resource (duh!) and IDR_KEK1 is a binary resource. Now, the thing is that all these calls, except the first one, fails. My wish is to load the binary resource but I have no idea what''s wrong with this from the start. Also, Im not really interested in getting the Ex-versions to work if the regular ones can do the job, although it would be interesting to learn why they fail atm. Thanks. "Far too often, we just sit around philosophizing about our feelings, opinions and theories instead of making real progress." //Bjarne Stroustrup
Maega
Maega
FindResource doesn't load any files. It just points to where a file is in the resourse section of your exe.

You'll have to use LockResource and LoadResource to do anything.

If you have to know the size of the resource you're trying to load, use SizeOfResource

Edit:
In addition, LoadBitmap exists to load a bitmap from resource. You don't have use FindResource et al to do it (unless maybe you are planning to write it to disk or something)



[edited by - Maega on September 6, 2003 12:07:28 AM]
Exellion
Exellion
quote:

In addition, LoadBitmap exists to load a bitmap from resource. You don't have use FindResource et al to do it (unless maybe you are planning to write it to disk or something)



Thanks, didn't know that. I'll try it right away.

Edit: Or wait a sec. That's just for bitmaps, as stated I'd like to load binary resources. Is there a way to do that without FindResource as well? I sure haven't heard of one. Find, Load and Lock the resource is how I planned to do it and if the Find-part fail, the other will too.

I added the bitmap-part of the above code to point of that FindResource works in some cases, but fails in others. But sure, if anyone knows how to load a binary resource using some other technique, Im eager to hear.

[edited by - Exellion on September 7, 2003 5:46:14 AM]
Maega
Maega
Well, I used to store binary data in my resource to be written to disk.

I used this simple function to get it out



BOOL WriteFileFromResource(int resourceid, LPCTSTR resourcetype, char* filename, bool createdirectory, char* directory)
{
if (createdirectory)
{
HRESULT result;
result = CreateDirectory(directory, NULL);

if (result == 0)
{
return FALSE;
}

result = SetCurrentDirectory(directory);

if (result == 0)
{
return FALSE;
}

}

HRSRC hRsrc;

hRsrc = FindResource(NULL, MAKEINTRESOURCE(resourceid), resourcetype);
if (hRsrc == NULL)
{
return FALSE;
}

HGLOBAL filedata;

filedata = LoadResource(NULL, hRsrc);

if (filedata == NULL)
{
return FALSE;
}

DWORD size;

size = SizeofResource(NULL, hRsrc);

if (size < 0)
{
return FALSE;
}

FILE* file;

file = fopen(filename, "wb");

if (file == NULL)
{
return FALSE;
}

fwrite(filedata, size, 1, file);

fclose(file);

return TRUE;
}



Edit: If the find part is failing, it's probably because the ID is wrong or the binary data is not in RT_BINARY. I always like to create my own section and place them there.

[edited by - Maega on September 7, 2003 11:43:52 AM]

Topic Locked

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

Sign in to reply to this topic.