5

I am creating a program to see if I can run a byte array in C#.

The program should grab a byte array "MyBinaryData" and Load+Run it as a new program. There will be a text box where you can enter the bytes to see the outcome (it's a experiment ;) ). I have 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 object is created in the application."

My second test was:

 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 needs to know what class in the byte array it needs to run.

I then tried:

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

But it gave me this: "System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application."

My program.cs is:

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());
    }
}

}

What other way can I do this to have the Whole program opened?

Thanks in advance.

user1270384
  • 711
  • 7
  • 24
  • 53
SteveLacy
  • 4,150
  • 2
  • 23
  • 30
  • Clearly you are not just typing random bytes, the byte[] you are using is an assembly, a Winforms one. Which does *not* like it when you call EnableVisualStyles() after the program already got started. Well, when the bytes do. – Hans Passant May 18 '12 at 00:58

1 Answers1

3

You have two way

first way make .exe from that byte array and then start it

second look at this execute byte array

Community
  • 1
  • 1
Likurg
  • 2,742
  • 17
  • 22