如何加载Win32可执行文件并从内存中运行?

时间:2013-09-17 11:14:59

标签: c# winapi memory

如果我有一个可执行文件作为一个进程中的资源嵌入,并且希望直接从内存运行该进程而不将其放在磁盘上。

static void Main()
{
const string pathOfExecutable = @"C:\WINDOWS\system32\notepad.exe"; //size = 67KB 
// read the bytes from the application EXE file
FileStream fs = new FileStream(pathOfExecutable, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] byteArray = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();


//Process.Start(byteArray) //Cannot use this, any other alternative?

}

3 个答案:

答案 0 :(得分:2)

Assembly.Load将加载.net程序集。记事本不是这样的事情。它是一个普通的原生Win32应用程序。使用Assembly.Load无法完成您尝试执行的操作。

答案 1 :(得分:2)

您的方法仅适用于托管可执行文件。 notepad.exe是本机可执行文件。要运行它,请使用Process类。

答案 2 :(得分:1)

一种解决方案是将内存内容写入临时文件(Path.GetTempFileName),然后使用Process.Start()

运行它
相关问题