Loading an EXE inside a TMemoryStream and executing it from there.


my question is very simple, I need to load an EXE file inside a TMemoryStream. Once the full image of the executable has been loaded in memory I need to execute it from there.

Reason? I need to bundle an EXE image inside my program and esecute it in-memory. It NEVER have to be written to disk.

I'm starting with 300 points but if the answer will show being harder than expected I will increase the points.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

not possible :-(
Not easily doable in-memory.  Running an EXE involves more than just loading it into memory, there's various kinds of relocation that have to go on, plus setting up a PSP, and more.

If you can make your executable very small, less than 64K and simple, it may compile into a .COM file, which is easier to load and run, but still not totally trivial.

Another way would be to write a special block-device driver, which looks to the system as a device, but actually transfers data from your internal .EXE.
NOT a trivial thing, and is going to be heavily system dependent (meaning you'll need separate drivers for DOS, Windows31, Windows 95/98/Me, Windows NT/2000/XP.)

Does this help?

hello BackDream, the windows system seems to be set up so that executables are only allowed to run from a disk file, there seems to be some good reasons for this. I guess anything is possible (I.E. creating your own driver to bypass the disk read, although delphi don't do drivers), so I'd have to agree with kretzschmar -> sort of Not posible, except maybe with very  advanced coding methods. But it seems that since you allready have an executable running, just do the code for your results and not launch another executable.
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

How about starting a console with command.com but in silent unseen mode using ShellExecute and SW_HIDE, send keys to it to start a memory disk, then copy the exe to it from stream, run it, and so on.

function ExecuteFile(const FileName, Params, DefaultDir: String; const ShowCmd: Integer): THandle;
  zFileName, zParams, zDir: array [0..250] of Char;
  Result := ShellExecute(Application.Handle,
    StrPCopy(zFileName, FileName),
    StrPCopy(zParams, Params),
    StrPCopy(zDir, DefaultDir),
    ShowCmd   // use SW_HIDE

Hi :-)

Is this other exe you want to run without extracting it to disk a delphi exe you have written?

If so then I suggest combining the 2 exe programs into one single exe.

It is easy to store another exe inside your main exe and then extract and run it... you could send it to windows/temp  or perhaps you could create a ram disk and place it there.

If you just want to keep people from being able to grab the extracted exe from the disk and run it themselves then you could protect the exe by using one of those utils that can make any exe a password protected exe... some of them may accept the password as a command line parameter... and so your main exe could send the pw but a user would not easily discover it.

If it is possible to store an exe in yours and somehow run it without putting it on disk first then I think the clues to how to do it might be found by examining the source code of an exe compressor like UPX. These compressors create a single file that has the de-compressor code at the start and the decompressor creates and writes the original exe into ram and runs it... this is close to what you need ... the decompressor code places an exe into ram and never writes it to disk... and yet the exe runs just fine :-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BackDreamAuthor Commented:
Thanks to all of you for your comments and... Gwena, I've downloaded the source code to UPX (C and assembler!) and the extractor, so the little program that extracts and RUNS the extracted program is all written in assembler.

Not understandable to me, unfortunately.

But id does exactly what I want to be able to do: bundle 2 exes, extract one directly into memory and run it from there...

I already know how to write all the compress, bundle, extract code. The only part I'm missing is how to run it once I have extracted it to memory.

Any more help would be so much appreciated!! :-)))

Hi BackDream :-)

I thought that that code was doing about what you wanted to do... I hoped maybe you could figure it out and tell me how to do it too :-)

Figuring out how UPX does that is too hard for my small brain :-)
Hi Gwena,
I've seen delphi exe binders that do just what your trying to do. Search for Multibinder on torry.net thats pretty much what you are looking for. Its freeware and comes with source!! I hope it sheds light on this.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       to accept Gwena's answer
Please leave any comments here within the next seven days.


Some days and here is the Christmas Time. I wish good luck and good health for you all and for your loved ones

EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.