从内存加载 Exe 时出现 TargetInitationException
我想加载我使用当前 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看异常的 InnerException 属性,了解导致代码爆炸的实际异常。
您使用的代码肯定是错误的,但实际上并不是失败的原因。 Fwiw,Main() 入口点是一个静态方法,您不创建 Program 类的实例。 method.Invoke(null, null) 是正确的方法。
但这是行不通的,您显然是在 Winforms 应用程序中运行此代码。您尝试加载的程序也是一个 Winforms 应用程序。并会尝试使用唯一的Application类对象。这是行不通的:
它可能看起来 当您从控制台模式应用程序尝试此操作时,这将起作用。实际上没有,控制台应用程序的 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:
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().
您正在
WindowsForm
应用的EntryPoint
上调用CreateInstance
,这是Main
方法。你不能那样做。如果您想在该二进制文件中创建某种类型的实例,请使用该类型的完全限定名称,以便能够创建它的实例。
如果您只想运行该应用程序,请使用 Process.Start(execomplete path);
You're calling
CreateInstance
onEntryPoint
of yourWindowsForm
app, which isMain
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);
由于 EntryPoint 是公共 + 静态的,因此您不需要/不应该实例化任何内容,只需:
如果“load.exe”是 GUI 应用程序,则将其加载到新的
AppDomain
中 - 有关示例,请参阅http://msdn.microsoft.com/en-us/library/ms173139.aspxSince the EntryPoint is public + static you don't need/should not instanciate anything, just:
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