process.start() embedded exe without extracting to file first c#

后端 未结 5 2218
南方客
南方客 2020-12-03 11:47

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

相关标签:
5条回答
  • 2020-12-03 12:07

    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.

    0 讨论(0)
  • 2020-12-03 12:10

    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.

    0 讨论(0)
  • 2020-12-03 12:17

    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

    0 讨论(0)
  • 2020-12-03 12:17

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

    • http://www.rohitab.com/discuss/topic/31681-c-run-program-from-memory-and-not-file/
    0 讨论(0)
  • 2020-12-03 12:26

    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

    0 讨论(0)
提交回复
热议问题