c# - 将字节数组作为新程序执行

标签 c# bytearray

我正在创建一个程序以查看是否可以在 C# 中运行字节数组。

该程序应获取字节数组“MyBinaryData”并将其作为新程序加载+运行。 将有一个文本框,您可以在其中输入字节以查看结果(这是一个实验 ;))。 我试过这个:

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

问题是它给出了这个错误: “System.Reflection.TargetInvocationException:......必须在应用程序中创建第一个 IWin32Window 对象之前调用 SetCompatibleTextRenderingDefault。”

我的第二个测试是:

 Assembly assembly = Assembly.Load(binaryData);

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

但这需要知道它需要运行的是字节数组中的什么类。

然后我尝试了:

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:必须在应用程序中创建第一个 IWin32Window 对象之前调用 SetCompatibleTextRenderingDefault。”

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

还有什么其他方法可以打开整个程序?

提前致谢。

最佳答案

你有两种方式

第一种方法是从该字节数组生成 .exe 然后启动它

再看这个execute byte array

关于c# - 将字节数组作为新程序执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10643430/

相关文章:

Java:对象到 byte[] 和 byte[] 到对象转换器( Tokyo Cabinet )

c# - C DLL 处理从 C# 调用的 FILE * - 传入和传出流

c# - 流媒体库 FFmpeg、avlib、libav 等

c# - 执行身份验证请求返回意外结果 : 404

c# - Windows Phone 8 在网络浏览器中搜索 bing 时停止打开 bing

java - 需要将 4 个字节的数组转换为 int

c# - 使用 Mono 和 Gtk 在 C# 中使用 PostgreSQL/MySQL 进行简单的 CRUD 操作#

c# - 使用 linq 进行验证/抛出异常

java - 如何检测字节数组到字符串转换中的字符串结尾?

c# - 从 byte[] 创建并保存图像会导致 Parameter is not valid 异常