开发者

TargetInvocationException when Loading Exe from memory

开发者 https://www.devze.com 2023-03-31 19:59 出处:网络
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:

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 = _BinaryRea开发者_如何学Pythonder.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);
        }



    }


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().


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消