2012-05-17 14 views
5

Sto creando un programma per vedere se riesco a eseguire un array di byte in C#.Esegui array Byte come nuovo programma

Il programma deve afferrare un array di byte "MyBinaryData" e caricare + eseguirlo come nuovo programma. Ci sarà una casella di testo in cui è possibile inserire i byte per vedere il risultato (è un esperimento;)). Ho provato questo:

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.... 
      } 

Il problema è che dà questo errore: "System.Reflection.TargetInvocationException: ...... SetCompatibleTextRenderingDefault deve essere chiamato prima il primo oggetto IWin32Window si crea nell'applicazione ".

La mia seconda prova era:

Assembly assembly = Assembly.Load(binaryData); 

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

Ma questo ha bisogno di sapere quale classe in array di byte di cui ha bisogno per funzionare.

Allora ho provato:

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

ma mi ha dato questo: "System.Reflection.TargetInvocationException: eccezione è stata generata dalla destinazione di una chiamata ---> System.InvalidOperationException:. SetCompatibleTextRenderingDefault deve essere chiamato prima che il primo oggetto IWin32Window venga creato nell'applicazione. "

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

}

Quale altro senso che posso fare questo per avere il programma tutto aperto?

Grazie in anticipo.

+0

Chiaramente non sono solo digitando byte casuali, il byte [] stai usando è un assembly, uno Winforms. A che * non * piace quando si chiama EnableVisualStyles() dopo che il programma è già stato avviato. Bene, quando i byte lo fanno. –

+0

Sì, non è casuale, ma è un codice estemporaneo. Grazie per il consiglio! – SteveLacy

risposta

3

avete due modo

primo modo exe fare da quella matrice di byte e quindi avviarlo

seconda occhiata a questo execute byte array

+0

Ho esaminato entrambi e la seconda opzione sembra vicina a ciò che voglio. Potresti spiegare un modo per usarlo nel mio progetto? Grazie – SteveLacy

+0

@AZInventor è il tuo programma C# di array di byte o no? – Likurg

+1

Leggi questo http://www.codeproject.com/Articles/18677/Dynamic-Assemblies-using-Reflection-Emit-Part-II-o – Likurg