0

I need to invoke an exe using System.Diagnostics.Process.Start(processInfo) and want to get some value return back.Based on return value i need to perform further operation. Exe is getting invoked and performing the task accurately, but i am not able to get return value back. Code gets stuck after process.Start() no exception or warning.

string arguments = arg[0]+ " " + arg[1] + " " + arg[2] + " " + arg[3];
string consoleExePath = @"C:\Test\Console.exe";
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.FileName = "cmd.exe";
processInfo.RedirectStandardOutput = true;
processInfo.UseShellExecute = false;
processInfo.WorkingDirectory = Path.GetDirectoryName(consoleExePath);
processInfo.Arguments = string.Format("/c START {0} {1}", Path.GetFileName(consoleExePath), arguments);
System.Diagnostics.Process p = System.Diagnostics.Process.Start(processInfo);
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
int result = p.ExitCode; // always 0

Code inside exe:

static int Main(string[] args)
{
  var flag = 0;
  flag=   objTest.DoSomething(args[0],args[1], args[2], args[3]);
  //Console.WriteLine("Process completed!");
  //Console.Read();
  //return flag;
  return 44; // a non zero value.
}

Edit: Due to Console.Read();, code execution got stuck. thanks to schnaader for catching the silly mistake. But var output = p.StandardOutput.ReadToEnd(); is still empty. tried int result = p.ExitCode; and result is always 0.

A Stacker
  • 71
  • 8
  • 4
    The .exe code calls `Console.Read()` which will wait for the user to enter a line, and only exit after that - did you take that into account? – schnaader Aug 14 '17 at 11:27
  • 1
    You're not launching your exe - you're launching `cmd.exe` which in turn is launching your exe, for some reason. The console process completed successfully and returned `0`. It doesn't pass back the return code of whatever *it* last executed. – Damien_The_Unbeliever Aug 15 '17 at 10:34
  • Damien_The_Unbeliever : could you suggest the changes required? – A Stacker Aug 15 '17 at 10:38
  • 1
    That depends - presumably, at some earlier point you had `.FileName = consoleExePath` and `.Arguments = arguments`. What, specifically, were you doing that made you decide to put `cmd` in the middle? – Damien_The_Unbeliever Aug 15 '17 at 10:40
  • Damien_The_Unbeliever : I am trying it without `cmd` . I ll post here abt it. Thanks. – A Stacker Aug 15 '17 at 10:52
  • Damien_The_Unbeliever : it worked, i removed `cmd`. Though `var output = p.StandardOutput.ReadToEnd();` still return blank value, but `p.ExitCode` returns the hard coded 'integer' value. Thanks. – A Stacker Aug 15 '17 at 14:27

1 Answers1

2

Your code works for me. Note that in your .exe code, there is a line:

Console.Read();

So the program will wait for the user to enter a line, and only exit after that. If you don't do this, the other code will wait for the application to terminate like you described.

So you should remove that line and try again.

Another possibility that Christian.K noted in the comments is to redirect standard input using processInfo.RedirectStandardInput = true. This way, you won't have to modify the .exe code. After that, you can redirect things to the standard input of your .exe, see MSDN for a full example.

schnaader
  • 49,103
  • 10
  • 104
  • 136
  • Alternatively, he could just use `RedirectStandardInput = true;` and then later, after `Start()`, `p.StandardInput.Close();`. – Christian.K Aug 14 '17 at 11:35
  • schnaader : Thanks, it was a very silly mistake. But still output is blank. i supposed to get integer value back. could you help again? – A Stacker Aug 15 '17 at 10:07
  • @AStacker: The process exit code (the `flag` in the .exe code) is available in `p.ExitCode` after the `p.WaitForExit()` call. – schnaader Aug 15 '17 at 10:18
  • schnaader: p.ExitCode returns 0 always. Even i have hard coded the return value to non-zero. – A Stacker Aug 15 '17 at 10:25
  • 1
    Ah, you're running "cmd /c start {0} {1}" which doesn't adopt the exit code from console.exe. Try running your exe directly, e.g. `processInfo.FileName = @"C:\Test\Console.exe"` and `processInfo.Arguments = arguments` – schnaader Aug 15 '17 at 10:48
  • Another possible approach could be this answer, but I don't know how to adapt this to C#: https://stackoverflow.com/questions/41778613/getting-the-return-value-from-a-cmd-c-command-in-vbscript – schnaader Aug 15 '17 at 10:49
  • schnaader : it worked, i removed `cmd`. Though `var output = p.StandardOutput.ReadToEnd();` still return blank value, but `p.ExitCode` returns the hard coded `integer` value. Thanks. – A Stacker Aug 15 '17 at 14:28