“参数计数不匹配";从内存运行exe时 [英] "Parameter count mismatch" on running exe from memory

查看:97
本文介绍了“参数计数不匹配";从内存运行exe时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的工作代码,用于从内存中运行一个简单的自制.Net可执行文件:

This is my working code for running a simple self-made .Net executable from memory :

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

但是此代码仅适用于微小的.exe文件,不适用于 putty 等其他可执行文件或其他较大的.net文件.

But this code works just for tiny .exe files not other executables like putty or other bigger .net files.

当我想使用另一个exe时说:

When i want to use another exe it says:

mscorlib.dll中发生了'System.Reflection.TargetParameterCountException'类型的未处理异常

An unhandled exception of type 'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll

其他信息:参数计数不匹配.

Additional information: Parameter count mismatch.

对于此行:method.Invoke(o, /*here*/ null);

问题:我该怎么办,我应该找什么?因为我对C#中的内存处理没有太多兴趣.我想从内存中为编程工具项目

Question: What can i do and What should i looking for ? Becuse im not much into Memory handling in c#. And i want to run bigger exe files from memory for a programming tools project

注意:我的工作示例是一个简单的c#代码,用于在控制台上打印字符串.

Note: My working sample was a simple c# code for printing a string on console.

更新:感谢Marc的回答,这是最终代码:

UPDATE: Thanks to Marc's answer, this the final code:

        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 (method == null) return;
        object[] parameters = method.GetParameters().Length == 0 ? null : new object[] { new string[0] };
        method.Invoke(null, parameters);

推荐答案

入口点是Main()方法或等效方法.为此允许有多个签名.您可以 使用无参数的Main(),但是您也可以使用Main(string[]).因此:您可能应该检查method(GetParameters())上的参数,并传入一些内容-大概是空的string[].

The entry-point is the Main() method or equivalent. There are multiple signatures allowed for this; you can have a parameterless Main(), but you can also have Main(string[]). So: you should probably check the parameters on the method (GetParameters()), and pass in something - presumably an empty string[].

请注意,入口点通常为static;不需要传入o,并且您现有的CreateInstance代码是毫无意义的(它将方法名传递给需要类型名的东西).您只需将null作为第一个参数即可.

Note that entry-points are generally static; there is no need to pass in an o, and your existing CreateInstance code is non-sensical (it passes a method name to something that expects a type name). You can just pass null as the first parameter.

这篇关于“参数计数不匹配";从内存运行exe时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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