Hack Forums
[.NET] Start C# console application from Byte Array - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Start C# console application from Byte Array (/showthread.php?tid=5923232)



Start C# console application from Byte Array - frakkio11299 - Dec 27, 2018

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, so it means that the bytes were read correctly. I tried various snippets that I found online, for example:
Code
Assembly a = Assembly.Load(programma);
             MethodInfo method = a.EntryPoint;
             if (method != null)
             {
                 object o = a.CreateInstance(method.Name);
                 method.Invoke(o, null);
             }

or:

Code
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 codes actually work with visual application c# (Winforms), but not with those console applications.
How can i make it works?


RE: Start C# console application from Byte Array - 0xDEAD10CC - Dec 28, 2018

Code
var appDomain = AppDomain.CreateDomain("ExecutionDomain");
appDomain.ExecuteAssembly("Program.exe");