Lilz
Lilz

Reputation: 4089

Run a method from an exe file

I need to call a method from an exe file

 ProcessStartInfo startInfo = new ProcessStartInfo(@"exeParser.exe");

        startInfo.WindowStyle = ProcessWindowStyle.Normal;
        startInfo.CreateNoWindow = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;


        startInfo.Arguments = ??

I don't know how to call a method and pass parameters

Any help please??

The executable is mine but I'm having trouble using the things in a web app so I thought it would be better to call it as a process

Thanks

Upvotes: 1

Views: 2845

Answers (2)

ChrisF
ChrisF

Reputation: 137178

Executables only have one entry point usually called "Main".

To call a specific method the application would have to expose a command line argument (or several) for the method name and its arguments. This would mean changing the application to interpret those arguments and call the appropriate method.

You will need to modify your "exeParser" to accept arguments and then act on those.

For example you'd could add:

\method [name] \arguments [1],[2],[3]

Then parse this to get the name and list of arguments.

If you only have one or two methods you could hardcode the switch:

switch (methodName)
{
     case "add":
         result = this.Add(arg1, arg2);
         break;
     case "subtract":
         result = this.Subtract(arg1, arg2);
         break;
     default:
         break;
}

If you have more or want to make the code more generic then you'd need to use reflection to get the method and call it.

Upvotes: 1

Mitch Wheat
Mitch Wheat

Reputation: 300728

You can't, unless the method is publicly exposed in an assembly.

Of course, if the executable were an un-obfuscated .NET .exe, then presumably you could use something like Reflector to disassemble the code and replicate it in your program (not recommended), BUT you would have to check the legality of doing that if you don't own the executable in question.

Upvotes: 0

Related Questions