简体   繁体   中英

Why does redirecting standard input result in stdout not being printed to a new console process?

So I am attempting to run a separate program, using Process.Start() .

The program will pause occasionally and wait for input. I wish to be able to send information to this process on standard input.

I am trying to make sense of the difference between creating a new process using the windows OS shell and the effects of redirecting standard input.

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 important information is just that if I redirect standard input, then no data displays to the screen.

Why is this the case?

Normally program.exe should be writing to its stdout file descriptor.

I assume that Process.Start() creates a console process. According to MSDN :

Creating a new console results in a new console window, as well as separate I/O screen buffers.

I assume that this new window contains the console and displays the data in the console buffer. Thus, if the program outputs to stdout , we should see that data on the screen.

This is what happens normally. Why does redirecting standard input result in nothing being displayed on the screen, even though I do not touch redirect standard output?

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 a console. A process can be attached to one console. A console can have many processes attached to it.

I thought that my process could just hook onto the console used by the simulator and then I could write to the console. However, it returns error code 6: the console handle is invalid.

So I got it working 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 be able to write to the window that the process is in. But I haven't found any way to do that. I can attach to the console of that process. But to write to that process, I require the console's screen buffer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM