in misc/ScreenBufferTest.cc [582:647]
static void testD(int argc, char *argv[]) {
if (!strcmp(argv[1], "D")) {
startChildProcess(L"D:parent");
return;
}
if (!strcmp(argv[1], "D:parent")) {
g_prefix = "parent: ";
HANDLE origBuffer = GetStdHandle(STD_OUTPUT_HANDLE);
writeTest(origBuffer, "<-- origBuffer -->");
HANDLE newBuffer = createBuffer();
writeTest(newBuffer, "<-- newBuffer -->");
setConsoleActiveScreenBuffer(newBuffer);
// At t=2, start a child process, explicitly forcing it to use
// newBuffer for its standard handles. These calls are apparently
// redundant on Windows 8 and up.
Sleep(2000);
trace("parent:----");
trace("parent: starting child process");
SetStdHandle(STD_OUTPUT_HANDLE, newBuffer);
SetStdHandle(STD_ERROR_HANDLE, newBuffer);
startChildInSameConsole(L"D:child");
SetStdHandle(STD_OUTPUT_HANDLE, origBuffer);
SetStdHandle(STD_ERROR_HANDLE, origBuffer);
// At t=6, write again to newBuffer.
Sleep(4000);
trace("parent:----");
writeTest(newBuffer, "TEST PASSED!");
// At t=8, close the newBuffer. In earlier versions of windows
// (including Server 2008 R2), the console then switches back to
// origBuffer. As of Windows 8, it doesn't, because somehow the child
// process is keeping the console on newBuffer, even though the child
// process closed its STDIN/STDOUT/STDERR handles. Killing the child
// process by hand after the test finishes *does* force the console
// back to origBuffer.
Sleep(2000);
closeHandle(newBuffer);
Sleep(120000);
return;
}
if (!strcmp(argv[1], "D:child")) {
g_prefix = "child: ";
// At t=2, the child starts.
trace("child:----");
dumpHandles();
writeTest("writing to newBuffer");
// At t=4, the child explicitly closes its handle.
Sleep(2000);
trace("child:----");
if (GetStdHandle(STD_ERROR_HANDLE) != GetStdHandle(STD_OUTPUT_HANDLE)) {
closeHandle(GetStdHandle(STD_ERROR_HANDLE));
}
closeHandle(GetStdHandle(STD_OUTPUT_HANDLE));
closeHandle(GetStdHandle(STD_INPUT_HANDLE));
Sleep(120000);
return;
}
}