12

I have an executable embedded into my app resources. ATM I use assembly reflection to extract the executable to its own file and then start the executive using process,START(). Is it possible to run the embedded executable straight from a stream instead of writing it to file first? Could someone please show me the most efficient way to do this please.

brux
  • 3,197
  • 10
  • 44
  • 78
  • Is the embedded executable (not executive, unless you embedded some piece of the government in there) a .NET executable or a native non-.net executable? – Lasse V. Karlsen May 13 '11 at 20:57
  • lol, ye, executable, predictive text was on the bus – brux May 13 '11 at 21:05
  • 2
    Creating a *separate* executable requires a file on disk. A hard Windows requirement that's fundamental to the way processes work. A simple bootstrapper that loads the assembly from the resource is of course possible. Users, IT staff and virus scanners otherwise like it when executables don't appear from thin air. – Hans Passant May 13 '11 at 21:05
  • Any time you want to actually ... you know ... **answer the question I asked** would be fine... really! I asked if you had a **.net** executable, not just *any* executable, you already said that. Please confirm that it is a .NET executable, or not. – Lasse V. Karlsen May 13 '11 at 21:09
  • oops i actually did mate, dunno what happened there tbh, its non .net my friend – brux May 13 '11 at 21:11

5 Answers5

9

Here's what I gather from your question, and your comments:

  1. You want to know if it is possible to execute an executable embedded into your program, without extracting it to disk first
  2. Your program is a .NET program
  3. The executable you want to execute is not a .NET program

The answer to that is: yes

However, the answer to that is also it is very, very, hard

What you have to do is, and note that I do not know all the details about this since I don't do this, but anyway:

  1. Load the executable code into memory
  2. Remap all addresses in the binary image so that they're correct in relation to the base address you loaded the executable at
  3. Possibly load external references, ie. other DLL's that executable need
  4. Remap the addresses of those references
  5. Possibly load references needed by the just loaded referenced DLL's
  6. Remape those dll's
  7. Repeat 3 through 6 until done
  8. Call the code

I'm assuming your question is "can I do 1 and 8", and the answer to that is no.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
4

if it's a .net executable, you should be able to load it into an appdomain and start it that way:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.load.aspx

Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
3

Very simple actually:

byte[] bytes = File.ReadAllBytes(path);
a = Assembly.Load(bytes);

Now instead of reading the bytes from a file, read it from the resource and you're done. Actually there is a very good article on that: Dynamically Loading Embedded Resource Assemblies

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
1

If you don't want it on a hard drive, you could possible look at saving it to a ram-drive and then run it from there.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
0

It can be done without your native EXE having to touch the disk.

See here....it shows an example of a "process" image being embedded as a Resource. It's read into memory, and then CreateProcess and a number of other things are done to build a valid running "process".

Colin Smith
  • 12,375
  • 4
  • 39
  • 47