views:

22

answers:

2

Hi

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

A: 

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.

Mitch Wheat
+1  A: 

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.

ChrisF
That's what I'm trying to do. I'm trying to call this exe froma project. send parameters and get results.
Lily
@Lily - then you need to find out what arguments `exeParser` accepts. Do you have any documentation for it?
ChrisF
I created it myself, so I can make it accept anything. I'm using it for the Proxem Antelope apiI have no idea how to return a result since main will only return void
Lily
String result = process.StandardOutput.ReadToEnd();I don't understand where the result is coming from if the main only returns void
Lily
@Lily - you need to modify your program to accept arguments that you can use to call specific methods. To get the output the easiest method might be to write it to a file in `exeParser` and then read that file in your calling program.
ChrisF
would I only be able to obtain strings from this ?
Lily
@Lily - they'd start out as strings, but you could convert them to integers or floats (for example) if you needed to using [Int32.Parse](http://msdn.microsoft.com/en-us/library/system.int32.parse.aspx) or [Double.Parse](http://msdn.microsoft.com/en-us/library/system.double.parse.aspx)
ChrisF