tags:

views:

314

answers:

6

Hello,

I have a program stored in byte array.

Is it possible to run it inside C#?

Thanks

A: 

If this "program in a byte array" is machine code, the answer is no.

Brent Arias
That's not exactly true, you could write it out as an .exe file and execute it.
Blindy
I was answering the question. He said run "inside" C#. If it is written as an exe and executed, then it is not "inside" C#.
Brent Arias
+6  A: 

Sure.

  1. Save the byte array to an .exe file.
  2. Use the Process class to execute the file.

Note: this is assuming that your byte array is executable code, and not source code. This also assumes that you have a valid PE header or know how to make one.

JSBangs
+1 simple and straightforward!
They might need to add executable header right?
m0s
Is it possible to do that but without actually saving byte array into exe file? Also yes it is executable.
DanSpd
@DanSpd, no. The OS doesn't allow you to execute a separate process from an in-memory executable. You can, with sufficient wizardry, execute in-memory code within your *own* process space, but this is highly not recommended. And it will make your program look like malware to many scanners, since this technique is often used by viruses and other undesirables.
JSBangs
You may not be able to spawn it in a second process, but you can run it in a new appdomain.
Matthew Whited
+1  A: 

You can create a virtual machine and execute the code OR you could use reflection and dynamic types to create a dynamic assembly, potentially. You can dynamically load assembly.

http://msdn.microsoft.com/en-us/library/system.reflection.assembly.load.aspx

You could thus perhaps do something with that. If my memory serves me though there are some limitations.

See

Reflection Assembly.Load Application Domain

Curtis White
+3  A: 

Yes. This answer shows you can directly execute the contents of a byte array. Basically, you use VirtualAlloc to allocate an executable region on the heap with a known address (a IntPtr). You then copy your byte array to that address with Marshal.Copy. You convert the pointer to a delegate with GetDelegateForFunctionPointer, and finally call it as a normal delegate.

Matthew Flaschen
+2  A: 

Assuming the byte array contains a .net assembly (.exe or .dll):

 Assembly assembly = AppDomain.Load(yourByteArray)
 Type typeToExecute = assembly.GetType("ClassName");
 Object instance = Activator.CreateInstance(typeToExecute);

Now, if typeToExecute implements an interface known to your calling program, you can cast it to this interface and invoke methods on it:

 ((MyInterface)instance).methodToInvoke();
ckarras
A: 

If the byte array is a .Net assembly with an EntryPoint (Main method) you could just do this. Most of the time returnValue would be null. And if you wanted to provide command line arguments you could put them in the commandArgs string listed below.

var assembly = Assembly.Load(assemblyBuffer);
var entryPoint = assembly.EntryPoint;
var commandArgs = new string[0];
var returnValue = entryPoint.Invoke(null, new object[] { commandArgs });
Matthew Whited