From 2d347ff5c5a6bc79be7d9a09612a2e503b3e65e0 Mon Sep 17 00:00:00 2001 From: Luminyx <27lumi@protonmail.com> Date: Sun, 15 Feb 2026 22:19:31 -0500 Subject: [PATCH] Don't re-attach stdio when piping output --- src/main.cpp | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9355465e..519b3b30 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -165,20 +165,25 @@ void UnitTests() } bool isConsoleConnected = false; -void requireConsole() -{ - #if BOOST_OS_WINDOWS - if (isConsoleConnected) - return; +void requireConsole() { + #if BOOST_OS_WINDOWS + if (isConsoleConnected) + return; - if (AttachConsole(ATTACH_PARENT_PROCESS) != FALSE) - { - freopen("CONIN$", "r", stdin); - freopen("CONOUT$", "w", stdout); - freopen("CONOUT$", "w", stderr); - isConsoleConnected = true; - } - #endif + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); + DWORD dwFileType = GetFileType(hOut); + + if (dwFileType == FILE_TYPE_UNKNOWN || dwFileType == FILE_TYPE_CHAR) { + if (AttachConsole(ATTACH_PARENT_PROCESS) != FALSE) { + freopen("CONOUT$", "w", stdout); + freopen("CONOUT$", "w", stderr); + freopen("CONIN$", "r", stdin); + isConsoleConnected = true; + } + } else { + isConsoleConnected = true; + } + #endif } void HandlePostUpdate()