开发者

Retrieving a resource file from an exe

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开发者_StackOverflow社区 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.


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜