【问题标题】:Execute Byte array as a new program将字节数组作为新程序执行
【发布时间】:2012-05-17 20:59:09
【问题描述】:

我正在创建一个程序来查看是否可以在 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:......SetCompatibleTextRenderingDefault 必须在应用程序中创建第一个 IWin32Window 对象之前调用。”

我的第二个测试是:

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

}

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

提前致谢。

【问题讨论】:

  • 显然您不只是输入随机字节,您使用的 byte[] 是一个程序集,一个 Winforms 程序集。在程序已经启动后调用 EnableVisualStyles() 时,not 喜欢它。好吧,当字节做的时候。
  • 是的,它不是随机的,而是实验代码。感谢您的提示!

标签: c# bytearray


【解决方案1】:

你有两种方式

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

再看看这个execute byte array

【讨论】:

  • 我都看过了,第二个选项看起来很接近我想要的。你能解释一下在我的项目中使用它的方法吗?谢谢
  • @AZInventor 是不是你的字节数组 C# 程序?
猜你喜欢
  • 2013-10-02
  • 2011-02-27
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多