Making CreateProcess inherit the console of the calling process Making CreateProcess inherit the console of the calling process windows windows

Making CreateProcess inherit the console of the calling process


I know, this thread is rather old, however, I just ran into the same problem.

Just as for the TS, the console handle was inherited and working fine under Cygwin, but not on a Windows console. Instead, the output on stdout was neither shown, nor any error was reported. Inherited Pipe handles worked still fine.

I took me some time to identify the (now obvious) problem: CreateProcess() was called with CREATE_NO_WINDOW. Dropping this flag, console output is fine. (Though, according to the code of the TS, they never set this flag in the first place.)

Hope this might be helpful for people who also stumble across this thread, like myself.


According to Microsoft documentation, lpCommandLine (2. parameter):

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.

When I stopped using a constant here it worked for me. I didn't need the STARTF_USESTDHANDLES and GetStdHandle thing.

This code from a console prg runs and outputs another console exe in the same console:

FillChar(SI, SizeOf(SI), 0);SI.cb:=SizeOf(SI);FillChar(PI, SizeOf(PI), 0);if CreateProcess(nil, CmdLineVar, nil, nil, False, 0, nil, nil, SI, PI) then ...


I've done this by passing in pipes for hStdInput, hStdOutput, and hStdError and manually routing data from the hStdOutput and hStdError pipes to the console.