Execute an array of bytes as a new program

I am creating a program to see if I can run an array of bytes in C #.

The program should capture an array of bytes "MyBinaryData" and Load + Run it as a new program. There will be a text box in which you can enter bytes to see the result (this is an experiment;)). I tried this:

 byte[] binaryData = System.IO.File.ReadAllBytes("MyBytes.txt");  // the bytes are in a .txt file for simple tests before becoming a textbox.
 Assembly LoadByte = Assembly.Load(binaryData);
        MethodInfo M = LoadByte.EntryPoint;

        if (M != null)
        {                object o = LoadByte.CreateInstance(M.Name);
            M.Invoke(o, new Object[] { null });  // this gives the error
        } 
        else {  
         ..... fail code here.... 
             } 

The problem is that it gives this error: "System.Reflection.TargetInvocationException: ...... SetCompatibleTextRenderingDefault must be called before the first IWin32Window is created in the application."

My second test:

 Assembly assembly = Assembly.Load(binaryData);

 Type bytesExe = assembly.GetType(); // problem: the GetType(); needs to know what class to run.
 Object inst = Activator.CreateInstance(bytesExe);

But this should know which class in the byte array it needs to run.

Then I tried:

var bytes = Assembly.Load(binaryData);
var entryPoint = bytes.EntryPoint;
var commandArgs = new string[0];
var returnValue = entryPoint.Invoke(null, new object[] { commandArgs });

: "System.Reflection.TargetInvocationException: . --- > System.InvalidOperationException: SetCompatibleTextRenderingDefault , IWin32Window."

program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Crypter
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form2());
    }
}

}

, ?

.

+5
1

.exe ,

+3

All Articles