Embed a .exe inside of a .exe then execute it.

Started by
19 comments, last by swiftcoder 16 years, 1 month ago
My chess program depends on several utility programs to handle opening books, tablebase compression, and other tasks. These utility programs are separate .exe files. I want to distribute my complete program as a single .exe file (call me oldschool. or crazy.) My chess engine runs on windows and is written in MSVC++. I've embedded other file types before. The thing I don't know how to do is embed an executable, load it into memory, then run it. I've seen C# examples that just write the embedded executable to disk, then ShellExecute it, but I'm thinking that in C++ I can do better. I might be looking for a piece of inline asm that just sets the pc register to an in-memory buffer. Googling has only brought up crap - I guess I don't know the right keywords. Any help greatly appreciated.

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

Advertisement
Just use ShellExecute - running an executable is ALOT more complicated than just dumping it into memory and jumping to the entry point. In particular you need to patch up any relocated sections, load DLLs, set up the import address table. All of these things are handled by the windows loader. Furthermore you would probably want to run your other executable in its own address space - which I doubt you can even set up from user mode.

There isn't a simple ShellExecuteFromMemory function.
Dump to disk and ShellExecute or CreateProcess it. As mentioned, you cannot start a separate process from user mode, at best you could load up the code and start it as a thread.

If you don't want that, you'll have to change the separate exes into libraries or DLLs and load them instead.
Quote:Original post by Telamon
My chess program depends on several utility programs to handle opening books, tablebase compression, and other tasks.

These utility programs are separate .exe files. I want to distribute my complete program as a single .exe file (call me oldschool. or crazy.)


You're old school and crazy :) This is just wrong!

Create libraries to do these tasks. Your utility programs and your main application should link against these. You may not even need the utility programs in the end as the functionality will be available in the main application via the libraries.

Quote:Googling has only brought up crap - I guess I don't know the right keywords.


Or, it *may* be because what you're trying to do isn't particularly sensible and you've created a problem for yourself that everyone else has avoided by doing things normally.
Quote:Original post by Telamon
I want to distribute my complete program as a single .exe file (call me oldschool. or crazy.)
That's not oldschool, or crazy.
Quote:My chess program depends on several utility programs to handle opening books, tablebase compression, and other tasks.
That's not oldschool or crazy either. But it is a bad idea.
Did you make these utilities yourself? If so, why not just use the code of them in your chess program?
If you insist on embedding these programs, you might want to have a look at this site: http://www.joachim-bauch.de/tutorials/load_dll_memory.html/en

The article and the library is about loading DLLs, not programs, but it should work anyway (no big difference).
After having loaded the data into memory (from a resource or whatever) and calling MemoryLoadLibrary successfully, MemoryGetProcAddress on main() should do the trick (or so I hope).
If you don't mind forking out some money for an easy way of doing this, then Molebox should do what you want.
Quote:Original post by samoth
The article and the library is about loading DLLs, not programs, but it should work anyway (no big difference).

You have no idea what you're talking about.
Quote:
After having loaded the data into memory (from a resource or whatever) and calling MemoryLoadLibrary successfully, MemoryGetProcAddress on main() should do the trick (or so I hope).

1) main() is not the entry point of an executable. Jumping to main would skip things like static initialization like that required for the C and C++ libraries to function properly. 2) main() isn't even guaranteed to exist. 3) If you did jump to the actual entry point rather than main(), the static initialization performed could clobber the static initialization performed by the host program, putting the host program into an unstable state.
Quote:Original post by samoth
After having loaded the data into memory (from a resource or whatever) and calling MemoryLoadLibrary successfully, ...


This would almost certainly not be successful as the preferred address for all EXEs is usually the same (0x400000 is the default), and EXEs are not relocatable (since they don't need to be).

This topic is closed to new replies.

Advertisement