这是我从内存中运行一个简单的自制.Net可执行文件的工作代码:
FileStream fs = new FileStream(@"simple.exe",FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();
Assembly a = Assembly.Load(bin);
MethodInfo method = a.EntryPoint;
if (method == null) return;
object o = a.CreateInstance(method.Name);
method.Invoke(o, null);
Run Code Online (Sandbox Code Playgroud)
但是这段代码只适用于微小的.exe文件而不是其他可执行文件,如putty或其他更大的.net文件.
当我想使用另一个exe时,它说:
mscorlib.dll中发生未处理的"System.Reflection.TargetParameterCountException"类型异常
附加信息:参数计数不匹配.
对于这一行: method.Invoke(o, /*here*/ null);
问题:我能做什么,我应该寻找什么?请原谅我在c#中的内存处理方面没什么了不起的.我想从内存中为编程工具项目运行更大的exe文件
注意:我的工作示例是一个简单的c#代码,用于在控制台上打印字符串.
更新:感谢Marc的回答,这是最终的代码:
FileStream fs = new FileStream(@"EverySample.exe", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();
Assembly a = Assembly.Load(bin);
MethodInfo method = a.EntryPoint;
if …Run Code Online (Sandbox Code Playgroud)