C# 从字节运行

发布于 2024-09-29 12:19:49 字数 1002 浏览 3 评论 0原文

我正在努力让我的客户端通过下载字节并使用反射打开另一个程序来打开它。我目前已在 C# 控制台应用程序上完成此操作,但当我尝试在 Windows 窗体应用程序上执行此操作时,出现此错误。

“调用目标已引发异常。”

这是代码

using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem.ToString() != null)
        {
            if (MessageBox.Show("Run " + listBox1.SelectedItem.ToString() + "?", "Run this program?", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                byte[] bytes;
                using (WebClient client = new WebClient())
                {
                    bytes = client.DownloadData(new Uri("http://example.net/program.exe"));
                }
                RunFromBytes(bytes);
            }
        }
    }
    private static void RunFromBytes(byte[] bytes)
    {
        Assembly exeAssembly = Assembly.Load(bytes);
        exeAssembly.EntryPoint.Invoke(null, null);
    }

I am working on making my client open another program by downloading the bytes and using reflection to open it. I have currently got this working on a C# Console Application, but when I try and do it on a Windows Form Application I get this error.

"Exception has been thrown by the target of an invocation."

Here is the code

using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem.ToString() != null)
        {
            if (MessageBox.Show("Run " + listBox1.SelectedItem.ToString() + "?", "Run this program?", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                byte[] bytes;
                using (WebClient client = new WebClient())
                {
                    bytes = client.DownloadData(new Uri("http://example.net/program.exe"));
                }
                RunFromBytes(bytes);
            }
        }
    }
    private static void RunFromBytes(byte[] bytes)
    {
        Assembly exeAssembly = Assembly.Load(bytes);
        exeAssembly.EntryPoint.Invoke(null, null);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您必须执行以下操作:

  1. 创建新的应用程序域
  2. 写入字节数组到一个文件
  3. 通过 ExecuteAssembly 执行它

这是代码:

File.WriteAllBytes("yourApplication.exe", bytes);
AppDomain newDomain= AppDomain.CreateDomain("newDomain");
newDomain.ExecuteAssembly("file.exe");

祝你好运!

You must do the following:

  1. Create a new application domain
  2. Write the byte array to a file
  3. Execute it by ExecuteAssembly

This is the code:

File.WriteAllBytes("yourApplication.exe", bytes);
AppDomain newDomain= AppDomain.CreateDomain("newDomain");
newDomain.ExecuteAssembly("file.exe");

Good luck!

葬心 2024-10-06 12:19:49

那是因为您正在尝试从另一个线程访问表单控件。
请参阅此处: http://www.yoda.arachsys.com/csharp/threads/ winforms.shtml

That's because you are trying to access your form controls from another thread.
see here: http://www.yoda.arachsys.com/csharp/threads/winforms.shtml

£冰雨忧蓝° 2024-10-06 12:19:49

你可以这样做:

private static void RunFromBytes(byte[] bytes)
{

Assembly exeAssembly = Assembly.Load(bytes);
var entryPoint = exeAssembly.EntryPoint;
var parms = exeAssembly.CreateInstance(entryPoint.Name);
 entryPoint.Invoke(parms, null);
}

You can do like:

private static void RunFromBytes(byte[] bytes)
{

Assembly exeAssembly = Assembly.Load(bytes);
var entryPoint = exeAssembly.EntryPoint;
var parms = exeAssembly.CreateInstance(entryPoint.Name);
 entryPoint.Invoke(parms, null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文