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.
-
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
-
2Creating 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 Answers
Here's what I gather from your question, and your comments:
- You want to know if it is possible to execute an executable embedded into your program, without extracting it to disk first
- Your program is a .NET program
- 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:
- Load the executable code into memory
- Remap all addresses in the binary image so that they're correct in relation to the base address you loaded the executable at
- Possibly load external references, ie. other DLL's that executable need
- Remap the addresses of those references
- Possibly load references needed by the just loaded referenced DLL's
- Remape those dll's
- Repeat 3 through 6 until done
- Call the code
I'm assuming your question is "can I do 1 and 8", and the answer to that is no.

- 380,855
- 102
- 628
- 825
-
1
-
2You're not, only the operating system guys are. Extract the image to disk. – Lasse V. Karlsen May 13 '11 at 21:17
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

- 46,929
- 26
- 130
- 185
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

- 25,584
- 6
- 69
- 80
-
1
-
ProcessStartInfo Class takes only string as parameter so if it isn't .NET, It can't be done without P/Invoke anyway. – Teoman Soygul May 13 '11 at 21:03
-
Well than you'll have to be inspired from polymorphic viruses which modify an in memory process to inject their own instructions but you know... – Teoman Soygul May 13 '11 at 21:11
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.

- 11,775
- 6
- 49
- 69
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".

- 12,375
- 4
- 39
- 47