-#if 0
- FILE * ctt = fopen("CON", "w");
- freopen("CON", "w", stdout);
- freopen("CON", "w", stderr);
-#endif
+ BOOL (WINAPI * AttachConsole)(DWORD dwProcessId);
+
+ AttachConsole = (BOOL (WINAPI *)(DWORD))
+ GetProcAddress(LoadLibraryA("kernel32.dll"), "AttachConsole");
+
+ if (AttachConsole != NULL && AttachConsole(((DWORD)-1)))
+ {
+ if (_fileno(stdout) == -1)
+ freopen("CONOUT$", "wb", stdout);
+ if (_fileno(stderr) == -1)
+ freopen("CONOUT$", "wb", stderr);
+ if (_fileno(stdin) == -1)
+ freopen("CONIN$", "rb", stdin);
+
+ // Fix C++
+ std::ios::sync_with_stdio();
+ }