Create a new EXE from within C
Am working in VC++ 2008 (express) and I would like to write something in C that cr开发者_如何学编程eates an "empty" exe that I can later call LoadLibrary
on and use BeginUpdateResource
, UpdateResource
, EndUpdateResource
to modify the contents.
Just writing a 0-byte file doesn't allow me to open it with LoadLibrary because it isn't a resource.
You can compile an empty .exe file with, for example,
int main() { return 0; }
and use it as a template. (Or an empty .dll, whatever)
The .EXE format is a complicated file format. It has a bunch of required headers just to describe its basic execution properties (16 bit, 32 bit or 64 bit, and DOS/Win16/Win32/Win64 mode and EXE versus DLL). After that, it has to have a correct table for address relocations. Its not trivial, and you have do some amount of research into the .EXE file format to do this properly.
"Creating" an exe is something the compiler is very good at. So why not have the compiler create the executable you want, and use that file (or a binary representation of it's contents) to copy around?
精彩评论