2

I can run a .net exe using this method :

  Assembly a = Assembly.Load(bytes);
MethodInfo method = a.EntryPoint;
if (method != null)
    method.Invoke(a.CreateInstance(method.Name), null);

How can I do the same thing for a non-.NET exe? I don't want to write .exe files and run it like Process.Start()

How can I accomplish this?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Sinaw
  • 61
  • 2
  • 9
  • What are you trying to do? Also see; http://stackoverflow.com/questions/19110747/loadlibrary-an-exe – Phil Price Jun 04 '14 at 20:03
  • Are you just trying to launch the EXE with bytes in memory or are you needing to inject it into your application as well? In your example, you are inherently loading your exe into your application. – TyCobb Jun 04 '14 at 20:07
  • It's possible, but *very* hard. You need to mimic the OS loader to do that, and it would be fragile. It's probably better to extract a temp directory. – vcsjones Jun 04 '14 at 20:11
  • 1
    @vcsjones can explain more about "mimic the OS loader" ?! – Sinaw Jun 04 '14 at 20:19
  • This question that you already asked has more backstory for what you are asking: http://stackoverflow.com/questions/24033288/run-a-program-from-an-array-of-bytes-without-creating-a-temporary-file-c-sharp?rq=1 – TyCobb Jun 04 '14 at 20:23

1 Answers1

3

If the program is unmanaged code, which a C or a C++ program for example, you cannot launch it directly from within .Net memory. What you can do is pack it as a resource, stream it to disk (a temp folder) and then start it with the System.Diagnostics.Process class. But it would show up in the process list.

If your program was a dll, you could use p/invoke to execute its entry point method.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321