IMPORTANT PART
I would like the program to be able to redirect input and output for both
parent and child processes. For example,
1 | timer "text.exe > test_output.txt" > timer_output.txt |
I managed to get the child process redirection working: remove the surrounding quotes, and pass it to CreateProcess in the lpCommandLine field. For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | CommandLine = "timer \"text.exe > test_output.txt\" > timer_output.txt"; ToBeRun = "text.exe > test_output.txt"; if(!CreateProcess("C:\\WINDOWS\\system32\\cmd.exe", // Module name ToBeRun, // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi) // Pointer to PROCESS_INFORMATION structure ) { PrintString("CreateProcess failed\n"); return 1; } |
But The parent process redirection is inscrutable. When I call GetCommandLine(), Windows has already stripped out any redirection operators. So that must mean they've already called SetStdHandle and redirected the input or output for me right? Well, it doesn't seem like it, because when I call GetStdHandle and WriteConsole in the parent process, it outputs to the console instead of the target file. But it gets weirder. If I use a redirection operator on the parent process, the child process handle is redirected, even though I have explicitly specified handle inheritance to be FALSE in CreateProcess(). I tried stepping through the outer calling processes in Visual Studio to find out where the redirection operators get cut out, but the key parts of the process seem to be in the hidden part of the kernel.
If you have any insights or suggestions or guesses or wild conspiratorial speculations about Microsoft's plans to drive all world's programmers except their own to insanity, please post them.
Sorry, this is probably too much of a beginner question for these forums, but stack overflow gave me no help and I've poured over MSDN articles for days to no avail, so I'm desperate.