Jump to content
  • Advertisement
Sign in to follow this  
Telamon

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

This topic is 3887 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

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Did you make these utilities yourself? If so, why not just use the code of them in your chess program?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!