Why does redirecting standard input cause stdout not to be printed in the new console process?
So, I am trying to start a separate program using Process.Start () .
The program is temporarily suspended and waiting for input. I want to be able to send information to this process on standard input.
I am trying to understand the difference between creating a new process using the Windows shell and the standard input redirection effects.
Exe UseShell RedirectSTDIN WINDOW Displays OutputDisplays
cmd.exe /c program Y N Y Y
N N Y Y
N Y Y N
program.exe Y N Y Y
N N Y Y
N Y Y N
The only important information is that if I redirect stdin, no data is displayed on the screen.
Why is this so?
Usually program.exe
it should be written to the file descriptor stdout
.
I am assuming it Process.Start()
creates a console process. According to
MSDN :
Creating a new console creates a new console window as well as separate I / O screen buffers.
I am assuming this new window contains the console and displays the data in the console buffer. Thus, if the program outputs to stdout
, we should see this data on the screen.
This is normal. Why does stdin redirection result in nothing being displayed on the screen even though I don't touch stdout redirection?
edit:
startInfo.WindowStyle = ProcessWindowStyle.Normal;
//startInfo.FileName = Path.Combine(BinaryDirectory, "program.exe");
startInfo.FileName = "cmd.exe";
startInfo.CreateNoWindow = false;
//startInfo.Arguments = "";
startInfo.Arguments = "/C program.exe" + startInfo.Arguments;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
According to the documentation here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681953(v=vs.85).aspx
A process can use the AttachConsole function to attach to the console. The process can be connected to one console. A console can have many processes attached to it.
I thought my process could just connect to the console used by the simulator and then I can write to the console. However, it returns error code 6: The console handle is not valid.
source to share
So I got it using Harry Johnson's method:
cmd.exe /c program.exe >CON 2>&1
I also believe that I could easily create my own window, hide the window for the process, and direct output to that new window.
I just wanted to write to the window this process is in. But I haven't found a way to do this. I can connect to the console of this process. But to record this process I need a console screen buffer
source to share