3

I use System.Diagnostics.Process to interact with a third party console application of which stdin/out/err have been redirected (the external program is written in C++ and I do not have no control over it).

ProcessStartInfo info = new ProcessStartInfo(fileName, arg); 
info.CreateNoWindow = false; // <- if true, stdin writes don't make it through
info.UseShellExecute = false;
info.RedirectStandardInput  = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError  = true;

var proc = new Process() { StartInfo = info };
proc.OutputDataReceived += new DataReceivedEventHandler(myOutputHandler);
proc.ErrorDataReceived  += new DataReceivedEventHandler(myErrorHandler);
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();

Later...

proc.StandardInput.WriteLine("some-short-command");

Works ok in a test console app when info.CreateNoWindow = false; But has no effect when info.CreateNoWindow = true;

The output and error redirection work fine in both cases.

The above code is part of a class library defining custom actions for FinalBuilder. The described behavior can be observed from a test console application or running from within FinalBuilder desktop application.

Interestingly when run from a third context - FinalBuilder server - with same user and environment, StandardInput.WriteLine has no effect, whether info.CreateNoWindow is true or false.

What is going on?

Can I make stdin redirection work regardless of the execution context?

oli
  • 682
  • 1
  • 12
  • 21
  • 2
    Console mode apps have *two* ways to write to the console. They can write to stdout, like *nix programs do. Or they can write using the native console api. Adding flavor like responding to single keypresses and adding color. The first one will redirect, the second one doesn't. You got the unlucky draw. – Hans Passant Jul 21 '13 at 23:44
  • Thanks for your insight. Is there any workaround to send input to such a console apps using the native console api? – oli Jul 22 '13 at 07:44
  • If native console api apps won't redirect I am confused because mine does redirect but not in all cases... I must be missing something here. – oli Jul 24 '13 at 13:28
  • 1
    Did you flush the standard input stream? `process.StandardInput.BaseStream.Flush();` – Christopher King Sep 14 '16 at 14:07

1 Answers1

1

Not sure why but explicitly specifying the user here solves the problem:

proc.UserName = user;
proc.Domain= domain;
proc.Password= password;

Not very elegant but works for me and may help someone.

oli
  • 682
  • 1
  • 12
  • 21