Recent

Author Topic: Loading File into memory and then executing it  (Read 8343 times)

Logic_Bomb

  • New Member
  • *
  • Posts: 41
Loading File into memory and then executing it
« on: May 04, 2012, 05:16:22 pm »
Hey all,

I was just wandering if anyone could help me with the following problem; I am creating an application that stores a separate compiled application within the application as a TLazarusResource. What I need to do is execute this separate application WITHOUT writing it to disk, I want to be able to load it straight into memory and then execute that application within memory. The application I wish to execute has no outside resources.

Any help would be very much appreciated!

Regards,

Logic_Bomb

aminer

  • Hero Member
  • *****
  • Posts: 956
Re: Loading File into memory and then executing it
« Reply #1 on: May 04, 2012, 07:43:45 pm »

Hello,


I have a little unit that does that, it is called URUNPE.pas

Here it is with a test file, go and download them:

http://pages.videotron.com/aminer/RunPE/URUNPE.PAS
http://pages.videotron.com/aminer/RunPE/test.pas


here is how you have to execute it:

runexe('c:\windows\system32\cmd.exe -9 1.pdf',urunpe.TByteArray(mem.memory),processhandle)


here you have to pass cmd.exe as a parameter with the other parameters,
here in this example i am passing -9 and 1.pdf to pigz.exe , after that
you have to pass also mem.memory that is a TMemoryStream that contains
your executable, and after that you pass a processhandle that is just a THandle
variable.


after that you have to call: waitforsingleobject(processhandle,infinite);
if you want to wait on the executable to be executed ..

and don't forget to  close the handle with:

closehandle(processhandle);



Amine Moulay Ramdane.



















aminer

  • Hero Member
  • *****
  • Posts: 956
Re: Loading File into memory and then executing it
« Reply #2 on: May 04, 2012, 07:55:48 pm »



If you don't understand something with urunpe.pas, just
tell me and i will try to explain it to you.


Amine Moulay Ramdane.

aminer

  • Hero Member
  • *****
  • Posts: 956
Re: Loading File into memory and then executing it
« Reply #3 on: May 04, 2012, 08:03:42 pm »


Here is the parameters that you have to pass to runexe() procedure:

First parameter: pass cmd.exe and its path and also pass the other
parameters that you have to pass to your executable.


Second parameter:  you have for exemple to store you executable into
a TMemoryStream called mem for exemple, and pass it to runexe() like
this:  urunpe.TByteArray(mem.memory)


Third parameter: pass a thandle variable.



Just look at the example that i gave you that uses
an executable called pigz.exe(parallel gzip) ...


If you have any other question just ask...



Amine Moulay Ramdane.









Amine Moulay Ramdane.
















invocker

  • Newbie
  • Posts: 1
Re: Loading File into memory and then executing it
« Reply #4 on: February 22, 2017, 08:00:53 am »
thank you amin it work perfetct in delphi 7 but not in delphi 2010 and height can you resolve it or send us a runpe that work in all Please
thank you  8)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12891
  • FPC developer.
Re: Loading File into memory and then executing it
« Reply #5 on: February 22, 2017, 10:58:48 am »
thank you amin it work perfetct in delphi 7 but not in delphi 2010 and height can you resolve it or send us a runpe that work in all Please
thank you  8)

It works in Free Pascal, and this is a lazarus/freepascal forum. Aminer is now active in comp.lang.pascal.misc

d.ioannidis

  • Full Member
  • ***
  • Posts: 233
    • Nephelae
Re: Loading File into memory and then executing it
« Reply #6 on: February 22, 2017, 12:30:10 pm »
You could test uExecFromMem ...

EDIT: Fixed link ...

regards,
« Last Edit: February 22, 2017, 03:12:02 pm by Dimitrios Chr. Ioannidis »

jacmoe

  • Full Member
  • ***
  • Posts: 249
    • Jacmoe's Cyber SoapBox
Re: Loading File into memory and then executing it
« Reply #7 on: February 22, 2017, 02:49:43 pm »
thank you amin it work perfetct in delphi 7 but not in delphi 2010 and height can you resolve it or send us a runpe that work in all Please
thank you  8)
A lot happens in 5 years. ;)
more signal - less noise

Thaddy

  • Hero Member
  • *****
  • Posts: 19238
  • Glad to be alive.
Re: Loading File into memory and then executing it
« Reply #8 on: February 22, 2017, 03:02:12 pm »
According to me, but I may be mistaken  O:-), you mean an executable? Like, loading a file in memory and executing it?  ;) Yes. the OS will do that for you, usually ::)
And NO I am not porting old code to work with newer Delphi's. Even such nice code as from delphibasics.info
If you pay attention you would already know why some (nice  8-) ) old code does  not work anymore. It is not because of the Delphi version, but because the OS won't allow it.
« Last Edit: February 22, 2017, 03:04:45 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1594
    • Lebeau Software
Re: Loading File into memory and then executing it
« Reply #9 on: February 22, 2017, 07:12:59 pm »
What I need to do is execute this separate application WITHOUT writing it to disk, I want to be able to load it straight into memory and then execute that application within memory.

While this is technically possible, it is not supported by the OS.  It requires running custom code that basically recreates the OS's native EXE loader.  A lot of things have to be done when loading an EXE to make it executable, and this get more complicated as an OS evolves.  Your best bet is to simply not do this.  Let the OS do its job normally.  If you don't want to save the file to disk (why not?), you could always create a temporary RAMDISK and run the extracted EXE from there.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018