Zachowanie FreeConsole w systemie Windows 8
W systemie Windows 8 mamy problem z FreeConsole. Wygląda na to, że zamyka uchwyty stdio bez zamykania strumieni plików.
Może to być problem z systemem Windows 8 lub może po prostu nie rozumiem (całkowicie absurdalnego) sposobu, w jaki podsystem aplikacji konsoli / interfejsu GUI działa.
Co się dzieje?
Minimalny przykład poniżej. Testowany z kompilatorami: VS2005, VS2013, VS2017, przy użyciu statycznie połączonego 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);
}