Estoy creando un programa para ver si puedo ejecutar una matriz de bytes en C#.Ejecutar matriz de bytes como un nuevo programa
El programa debería tomar una matriz de bytes "MyBinaryData" y Cargar + Ejecutar como un nuevo programa. Habrá un cuadro de texto donde puede ingresar los bytes para ver el resultado (es un experimento;)). He intentado esto:
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....
}
El problema es que da este error: "System.Reflection.TargetInvocationException: ...... SetCompatibleTextRenderingDefault debe ser llamado antes de que se crea el primer objeto en la aplicación IWin32Window "
Mi segunda prueba fue:
Assembly assembly = Assembly.Load(binaryData);
Type bytesExe = assembly.GetType(); // problem: the GetType(); needs to know what class to run.
Object inst = Activator.CreateInstance(bytesExe);
Pero esto tiene que saber qué clase en el conjunto de bytes que necesita para funcionar.
Luego probé:
var bytes = Assembly.Load(binaryData);
var entryPoint = bytes.EntryPoint;
var commandArgs = new string[0];
var returnValue = entryPoint.Invoke(null, new object[] { commandArgs });
pero me dio esto: "System.Reflection.TargetInvocationException: Se inició una excepción por el destino de una invocación ---> System.InvalidOperationException:. SetCompatibleTextRenderingDefault debe ser llamado antes de que se cree el primer objeto IWin32Window en la aplicación. "
Mis Program.cs es:
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());
}
}
}
qué otra forma puedo hacer esto a haber abierto el programa entero I?
Gracias de antemano.
Es evidente que usted no está solo teclear bytes aleatorios, el byte [] lo que estás usando es un ensamblado, uno de Winforms. Lo cual no le gusta cuando llama a EnableVisualStyles() después de que el programa ya comenzó. Bueno, cuando los bytes lo hacen. –
Sí, no es un código aleatorio, sino explícito. ¡Gracias por el consejo! – SteveLacy