I want to start a C# console application by loading it from a byte array.
If I write the application to disk and start the program manually, it starts, which means that the bytes were read correctly.
I tried various snippets that I found online, for example:
Assembly a = Assembly.Load(programma);
MethodInfo method = a.EntryPoint;
if (method != null)
{
object o = a.CreateInstance(method.Name);
method.Invoke(o, null);
}
or:
public static Thread RunFromMemory(byte[] bytes)
{
var thread = new Thread(new ThreadStart(() =>
{
var assembly = Assembly.Load(bytes);
MethodInfo method = assembly.EntryPoint;
if (method != null)
{
method.Invoke(null, null);
}
}));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return thread;
}
These two code examples work with a WinForms application, but not with a console application.