views:

57

answers:

2

I'm wrapping files with Visual C++ 2008, I've figured out how to embed them but I can't figure out how to retrieve them. I have some C++ experience, but none with Win32 or Visual C++. The goal of the wrapping is to run some code, and then if everything okay it can run the embedded file.

I'm wrapping many different files, so code reuse is key, and in all cases I won't know the name of the embedded file. But I could name the exe the same as the wrapped file, so if the program can get the name of itself that'd work too.

Some of the wrapped files will be exes, and the others will be files meant to be run by an external program.

Edit: These files are being embedded with a .res file, they're not just concatenated to the end of the exe.

+4  A: 

So you have a binary file embedded as a resource in an EXE, and you want to read the file?

Try something like this (very rough, look up functions on MSDN for proper parameters):

HRSRC hResource = FindResource(NULL, MAKEINTRESOURCE(id), type);
HGLOBAL hGlobal = LoadResource(NULL, hResource);
BYTE* pData = (BYTE*)LockResource(hGlobal);
int size = SizeofResource(NULL, hResource);
// ... do something with pData and size, eg write to disk ...
FreeResource(hGlobal); // done with data

You'll want to add some error checking in to that!

AshleysBrain
you might want to fix your variable usages. theResource->hResource, theData->hGlobal...
Bahbar
Oops, thats what I get for copy-pasting out another application too quick. Fixed...
AshleysBrain
For the first line I have id as "test.exe" and type is RT_RCDATA. It compiles but according to the Debugger hResource is null. I'm not sure what is wrong...
jonescb
Did you check GetLastError()?
AshleysBrain
A: 

The main thing you need to know (which should be present in the .RC file that gets compiled to the .res file) is the name of the resource. From that, you can use FindResource, and LoadResource to load the data. You'll apparently write that data out to a temporary file and execute that file.

Jerry Coffin