在Windows 8上,我们遇到了使用FreeConsole的问题。它似乎关闭了stdio句柄,但没有关闭文件流。这可能是一个Windows 8的问题,或者只是我不理解Windows控制台/GUI应用程序子系统的(完全荒谬的)工作方式。下面是一个最简示例,使用VS2005、VS2013和VS2017编译器进行测试,使用静态链接CRT。
#include <windows.h>
#include <io.h>
#include <stdio.h>
static void testHandle(FILE* file) {
HANDLE h = (HANDLE)_get_osfhandle(fileno(file));
DWORD flags;
if (!GetHandleInformation(h, &flags)) {
MessageBoxA(0, "Bogus handle!!", "TITLE", MB_OK);
}
}
int main(int argc, char** argv)
{
freopen("NUL", "wb", stdout); // Demonstrate the issue with NUL
// Leave stderr as it is, to demonstrate the issue with handles
// to the console device.
FreeConsole();
testHandle(stdout);
testHandle(stderr);
}