JDOE JDOE - 23 days ago 21
C# Question

Parameter Count Mismatch when loading exe entrypoint C#

I have a .net exe in a byte array. I load it as assembly and try to invoke it's entry point

var assembly = System.Reflection.Assembly.Load(buff);
var entryPoint = assembly.EntryPoint;
var commandArgs = new string[0];
var returnValue = entryPoint.Invoke(null, new object[] { commandArgs });


with my test exe witch is some silly command line app that outputs "hello", it works. With some other third party exe however it fails with
Parameter Count Mismatch


at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at ConsoleApplication1.Program.a() in C:\Documents and Settings\user\Desktop\OLD\ConsoleApplication8\ConsoleApplication8\Program.cs:line 211
at ConsoleApplication1.Program.Main(String[] args) in C:\Documents and Settings\user\Desktop\OLD\ConsoleApplication8\ConsoleApplication8\Program.cs:line 41


Why?

Here is what I know for that exe - it is .net, it has public static void main() method and it is correctly identified as the entry point and it is relatively small and simple app in a single exe.

Answer

In .NET you can declare entry point either with single parameter of type string[] (such as static void Main(string[] args)) or with no paramaters (such as static void Main()). Exe file you are trying to run probably uses the latter method, so you can't pass any commandline arguments to it - at least that's what error message tells you - wrong number of parameters.

So to run it, you would have to call it without arguments:

var returnValue = entryPoint.Invoke(null, new object[] { });
Comments