CreateProcess with a new console window, but override some std I / O descriptors

If you use CreateProcess with the CREATE_NEW_CONSOLE flag, the new process has its own standard input, output, and error commands directed to a new console window. If you want to redefine I / O streams, you can do this by setting the descriptors in the STARTUPINFO hStdOutput, hStdInput and hStdError fields and setting the STARTF_USESTDHANDLES flag.

But what if you want to override only one of the descriptors? For example, I could redirect stderr to a file, leaving stdout and stdin connected to a new console window.

The STARTF_USESTDHANDLES flag tells CreateProcess to replace all descriptors instead of connecting them to new console windows. Looks like we should provide all three pens. Obviously, I can set hStdError to the log file descriptor, but what values ​​should be used for hStdInput and hStdOutput?

I tried using NULL, which seems to work on Windows 8.1, but it does not work on Windows 7.

I also thought about creating a console window first, and then called CreateProcess with the descriptors of the new console window buffers (and lowered the CREATE_NEW_CONSOLE flag). Unfortunately, the parent process is also a console application, and it looks like the console application cannot create a second console window.

+4
source share
3 answers

MSDN:

, GetStdHandle() , , . , , hStdInput STARTUPINFO :

hStdInput = GetStdHandle(STD_INPUT_HANDLE);

GetStdHandle() :

STD_INPUT_HANDLE
(DWORD) -10
. , CONIN $.

STD_OUTPUT_HANDLE
(DWORD) -11
. , CONOUT $.

STD_ERROR_HANDLE
(DWORD) -12
. , CONOUT $.

...

SetStdHandle, GetStdHandle . , CONIN $ CreateFile, . , CONOUT $, .

/

, STARTF_USESTDHANDLES.

NULL, - , .

CREATE_NEW_CONSOLE STARTF_USESTDHANDLES , , NULL - .

, STDIN , GetStdHandle(STD_INPUT_HANDLE) NULL, -, CONIN$. STARTUPINFO, STDIN . , STDIN , GetStdHandle(STD_INPUT_HANDLE) /pipe/etc, .

STDOUT STDERR.

, STDIN/OUT/ERR, hStdInput/Output/Error . , , GetStdHandle() CreateProcess(), , , , .

+4

Windows 7, , ( ) . , .

, :

  • CONOUT $(), ( CONOUT $ 7), .

  • , , , - 7. CONOUT $, . 7, CONOUT $, 7.

  • , CONOUT $ 7 ( ). STARTF_USESTDHANDLES, , 7 . CONOUT $, , .

  • , , CONOUT $. , , cmd.exe . , CreateProcess, .

  • CONIN $; CONIN $ 3. ( , .)

, , , , , , , .

- CREATE_NEW_CONSOLE STARTF_USESTDHANDLES. , , , , , .

+2

If I have correctly interpreted this documentation , you should use GetStdHandle(STD_INPUT_HANDLE)and GetStdHandle(STD_OUTPUT_HANDLE)for the other two descriptors.

+1
source

All Articles