INTRO(SKIP ME) I'm a very new programmer and have been following Handmade Hero avidly for a few months. I'm writing a very simple timer utility, called from the command line, that can be used to benchmark batch files or small console executables. I wanted to give myself a challenge, so in keeping with the handmade spirit I wrote the thing without the C standard library
(thanks Martinš Možeiko!). It turned out to be very difficult for me because I had to print PerfCount / PerfFreq. This involved weeks of trying to wrap my head around the
algorithms for precisely
printing floating point
numbers (still an unsolved problem surprisingly enough!). I eventually just hacked it by multiplying the numerator by 100000000000 or so and using 128 bit integer division.
IMPORTANT PART
I would like the program to be able to redirect input and output for both
parent and child processes. For example,
| timer "text.exe > test_output.txt" > timer_output.txt
|
should measure the time it takes for test.exe to output its data to test_output.txt and print that time measurement to 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.