Link to home
Start Free TrialLog in
Avatar of BackDream
BackDream

asked on

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

Hello,

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.

THANKS IN ADVANCE!!
Avatar of kretzschmar
kretzschmar
Flag of Germany image

not possible :-(
Avatar of grg99
grg99

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.
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;
var
  zFileName, zParams, zDir: array [0..250] of Char;
begin
  Result := ShellExecute(Application.Handle,
    nil,
    StrPCopy(zFileName, FileName),
    StrPCopy(zParams, Params),
    StrPCopy(zDir, DefaultDir),
    ShowCmd   // use SW_HIDE
    );
end;

Regards
Steve
ASKER CERTIFIED SOLUTION
Avatar of Gwena
Gwena

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BackDream

ASKER

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

BackDream
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

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

kacor
EE Cleanup Volunteer