Sign in to follow this  

Packing files into EXE's

This topic is 4190 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone have a simple explanation of how to do this? (Visual Studio (2005), C++, Direct3D). I would like to embed my .X meshes, textures, etc. into the .EXE, and load them from within the application. Thanks.

Share this post


Link to post
Share on other sites
It requires two steps (informations are for VS.NET 2003; VS.NET 2005 should be similar):

1) storing the info in the EXE.
Not difficult: in the resource panel of VC++, right click the project, select the Add Resource menu option. In the Add Resource dialog, click "Import...", select your file. It will ask you a custom resource type name - put whatever you want (MESH, TEXTURE, and so on) and voilà, you're done.
Alternatively, you can add a single line in the .rc file:
YOUR_RESOURCE_ID     YOUR_CUSTOM_RESOURCE_TYPE    "path"

YOUR_RESOURCE_ID should be defined in resource.h. YOUR_CUSTOM_RESOURCE_TYPE is an uppercase resource type name (MESH, TEXTURE, and so on).
Compile: the new file is added to the resource section of your exe.

2) reading the resource
Not difficult either. FindResource() will locate the resource in the exe and returns a HRSRC. LoadResource() will use this HRSRC to read the resource and will return an allocated memory zone (a HGLOBAL), and LockResource() will use this HGLOBAL to retrieve a pointer to the allocated memory.

// get the application hInstance
HMODULE hinst = (HMODULE)(GetModuleHandle(NULL));

HRSRC hr = FindResource(hinst,
MAKEINTRESOURCE(YOUR_RESOURCE_ID),
"YOUR_CUSTOM_RESOURCE_TYPE");

if (hr != NULL) {
HGLOBAL mem = LoadResource(hinst, hr);
if (mem != NULL) {
char *c = (char *)(LockResource(mem));
if (c == NULL) {
// this shouldn't happen!
OutputDebugString("no data ? Strange...\n");
}
UnlockResource(mem); // don't forget this!
}
}


HTH,

Share this post


Link to post
Share on other sites
That UnlockResource() call seems to not be in msdn. It is #defined as follows:

#define UnlockResource(hResData) ((hResData), 0)

What does this mean?

Also, is there a way to detect how large the resource is, or where the end of the file is?

Share this post


Link to post
Share on other sites
From the Platform SDK Documentation:
Quote:
It is not necessary to unlock resources because the system automatically deletes them when the process that created them terminates.


Edit: But there are DeleteObject(), DestroyAcceleratorTable(), DestroyCursor(), DestroyIcon(), and DestroyMenu() to free system resources before program termination.

Also:
Quote:
Also, is there a way to detect how large the resource is…?

There is SizeofResource().

Share this post


Link to post
Share on other sites
Good to know, thanks.

One question still remains for me:

What's a good way to selectively choose at compile-time which resources you want to load into the exe? Defining global symbols and using #ifdefs in the .rc seems to work.

This wouldn't actually work for me, as I need to load a resource which references other resources!

Is there a traditional way?

EDIT: EnumResourceNames() might do the trick...

[Edited by - discman1028 on June 23, 2006 5:38:12 PM]

Share this post


Link to post
Share on other sites
I guess my question is:


If I have this in the .rc:

IDR_TEXT1 TEXT "C:\\text.txt"

and this in the resource.h:

#define IDR_TEXT1 107


Is there any way for me to find "107" from a string I have at runtime (If I had either "IDR_TEXT1" or "C:\text.txt" in a std::string, say)?

Share this post


Link to post
Share on other sites
I strongly doubt it. As far as your program is concerned, the number 107 is the name of the resource. This is what you need to use to find it.

Share this post


Link to post
Share on other sites
If you want such maps, why not write a quick program (in either C++ or your favourate scripting language) that generates your resource .h and .rc files, and some C++ code that exposes a map from name->resource_id?

Code that writes code is your friend.

Share this post


Link to post
Share on other sites

This topic is 4190 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this