Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2003-02-19
Medium Priority
Last Modified: 2010-04-04

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.

Question by:BackDream
LVL 27

Expert Comment

ID: 7982878
not possible :-(
LVL 22

Expert Comment

ID: 7983059
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?

LVL 34

Expert Comment

ID: 7983678
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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 7984635
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


Accepted Solution

Gwena earned 1200 total points
ID: 7986131
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 :-)

Author Comment

ID: 7991967
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!! :-)))


Expert Comment

ID: 7995930
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 :-)

Expert Comment

ID: 9210960
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.
LVL 10

Expert Comment

ID: 9928045
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Integration Management Part 2
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question