从内存加载 Exe 时出现 TargetInitationException

发布于 2024-12-01 19:08:14 字数 789 浏览 0 评论 0原文

我想加载我使用当前 Windows 窗体应用程序创建的另一个 C# Windows 窗体应用程序。我想从内存中加载它。但是,我遇到了:

mscorlib.dll 中发生了类型为“System.Reflection.TargetInvocationException”的未处理异常 附加信息:调用目标已引发异常。

    private void button1_Click(object sender, EventArgs e)
    {

        FileStream _FileStream = new FileStream("load.exe", FileMode.Open);
        BinaryReader _BinaryReader = new BinaryReader(_FileStream);
        byte[] bBytes = _BinaryReader.ReadBytes(Convert.ToInt32(_FileStream.Length));

        _BinaryReader.Close();
        _FileStream.Close();

        Assembly a = Assembly.Load(bBytes);
        MethodInfo method = a.EntryPoint;


        if (method != null)
        {
            object o = a.CreateInstance(method.Name);
            method.Invoke(o,null);
        }



    }

I want to load another C# windows form application that I created with my current windows form application. I want to load it from memory. However, I am running into:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an invocation.

    private void button1_Click(object sender, EventArgs e)
    {

        FileStream _FileStream = new FileStream("load.exe", FileMode.Open);
        BinaryReader _BinaryReader = new BinaryReader(_FileStream);
        byte[] bBytes = _BinaryReader.ReadBytes(Convert.ToInt32(_FileStream.Length));

        _BinaryReader.Close();
        _FileStream.Close();

        Assembly a = Assembly.Load(bBytes);
        MethodInfo method = a.EntryPoint;


        if (method != null)
        {
            object o = a.CreateInstance(method.Name);
            method.Invoke(o,null);
        }



    }

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

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

发布评论

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

评论(3

梦明 2024-12-08 19:08:14

查看异常的 InnerException 属性,了解导致代码爆炸的实际异常。

您使用的代码肯定是错误的,但实际上并不是失败的原因。 Fwiw,Main() 入口点是一个静态方法,您不创建 Program 类的实例。 method.Invoke(null, null) 是正确的方法。

但这是行不通的,您显然是在 Winforms 应用程序中运行此代码。您尝试加载的程序也是一个 Winforms 应用程序。并会尝试使用唯一的Application类对象。这是行不通的:

  • Application.EnableVisualStyles() 将失败,必须在创建任何窗口之前调用它
  • Application.Run() 将失败,只能有一个活动消息循环

它可能看起来 当您从控制台模式应用程序尝试此操作时,这将起作用。实际上没有,控制台应用程序的 Main() 方法没有 [STAThread] 属性。 GUI 应用程序的硬性要求。没有它,许多典型的 GUI 操作都会以神秘的方式失败。任何使用剪贴板、拖放的内容(例如 OpenFileDialog 等 shell 对话框)都需要 STA 线程。

这根本就不会飞。考虑 Process.Start()。

Look at the exception's InnerException property to know the actual exception that made the code bomb.

The code you used is definitely wrong but not actually the reason for the failure. Fwiw, the Main() entrypoint is a static method, you don't create an instance of the Program class. method.Invoke(null, null) is the correct way.

But it isn't going to work, you are obviously running this code in a Winforms app. The program you're trying to load is also a Winforms app. And will try to use the one-and-only Application class object. That cannot work:

  • Application.EnableVisualStyles() will fail, it must be called before any windows are created
  • Application.Run() will fail, there can only be one active message loop

It may look like this will work when you try this from a console mode application. It actually doesn't, the Main() method of a console app doesn't have the [STAThread] attribute. A hard requirement for GUI apps. Without it, lots of typical GUI operations will fail in mysterious ways. Anything that uses the clipboard, drag and drop, the shell dialogs like OpenFileDialog for example requires an STA thread.

This just isn't going to fly. Consider Process.Start().

流心雨 2024-12-08 19:08:14

您正在 WindowsForm 应用的 EntryPoint 上调用 CreateInstance,这是 Main 方法。你不能那样做。

如果您想在该二进制文件中创建某种类型的实例,请使用该类型的完全限定名称,以便能够创建它的实例。

如果您只想运行该应用程序,请使用 Process.Start(execomplete path);

You're calling CreateInstance on EntryPoint of your WindowsForm app, which is Main method. You can not do that.

If you want to create an instance of some type inside that binary, use fully qualified name of that type in order to be able to create an instance of it.

If you want just run that app, use Process.Start(exe complete path);

百善笑为先 2024-12-08 19:08:14

由于 EntryPoint 是公共 + 静态的,因此您不需要/不应该实例化任何内容,只需:

a.EntryPoint.Invoke(null,null);

如果“load.exe”是 GUI 应用程序,则将其加载到新的 AppDomain 中 - 有关示例,请参阅http://msdn.microsoft.com/en-us/library/ms173139.aspx

Since the EntryPoint is public + static you don't need/should not instanciate anything, just:

a.EntryPoint.Invoke(null,null);

IF the "load.exe" is a GUI app then load it into a new AppDomain - for an example see http://msdn.microsoft.com/en-us/library/ms173139.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文