加载一个 EXE 文件并从内存中运行它 [英] Load an EXE file and run it from memory

查看:49
本文介绍了加载一个 EXE 文件并从内存中运行它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从内存中运行一个可执行文件,例如 this 文章.我可以很容易地运行任何 .net/managed exes.但我无法运行诸如 notepad.execalc.exe 之类的可执行文件.如何获得它以便我也可以运行非托管的 exe?

I'm trying to run an executable from memory such as outlined in this article. I can run any .net/managed exes quite easily. But I cannot run executables such as notepad.exe or calc.exe. How can I get it so I can also run unmanaged exes?

推荐答案

在从内存运行 .NET 可执行文件的情况下,库和 CLR 本身为您做了很多繁重的工作.对于像 notepad.exe 和 calc.exe 这样的本机可执行文件,您必须进行大量手动工作才能实现.基本上,您必须像 Windows 加载程序一样工作.

In the case of running .NET executables from memory, the libraries and CLR itself are doing a lot of heavy lifting for you. For native executables like notepad.exe and calc.exe, you'll have to do a lot of manual work to get it to happen. Basically, you have to act like the Windows loader.

这里可能有几页警告,但是这个-深度文章包含加载 PE 所需的步骤 wiki, msdn进入内存并进行正确的变基和修正.然后,您应该能够找到入口点(如文章中所述)并运行它.

There's probably pages of caveats here, but this in-depth article has the steps you need to load a PE wiki, msdn into memory and do the correct rebasing and fixups. Then, you should be able to find the entry point (like in the article) and run it.

如果您真的只想运行 notepad.exe 和 calc.exe,最简单的方法当然是使用 Process.Start 并在磁盘上运行它们.否则,如果您将可执行文件作为资源嵌入到您的进程中,那么下一个最简单的方法是将内容写入磁盘的临时位置(请参阅 Path.GetTempFileName) 然后从那里执行它.

If you're really just wanting to run notepad.exe and calc.exe, the easiest way, of course, would be to use Process.Start and run them off disk. Otherwise, if you have an executable embedded as a resource in your process, then the next easiest way would be to just write the contents out to disk in a temporary location (see Path.GetTempFileName) and then execute it from there.

这篇关于加载一个 EXE 文件并从内存中运行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆