From 2b4216337924dd52f5c6d4189a95110279185596 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Thu, 29 Sep 2022 00:22:51 -0400 Subject: [PATCH] Bind stdout and stderr for windows so that the console can get output --- libs/kiplatform/msw/app.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/libs/kiplatform/msw/app.cpp b/libs/kiplatform/msw/app.cpp index 3ed7cd9ae1..f70959f710 100644 --- a/libs/kiplatform/msw/app.cpp +++ b/libs/kiplatform/msw/app.cpp @@ -61,6 +61,26 @@ bool KIPLATFORM::APP::Init() // Moves the CWD to the end of the search list for spawning processes SetSearchPathMode( BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE | BASE_SEARCH_PATH_PERMANENT ); + // In order to support GUI and CLI + // Let's attach to console when it's possible + HANDLE handleStdOut, handleStdErr; + if( AttachConsole( ATTACH_PARENT_PROCESS ) ) + { + handleStdOut = GetStdHandle( STD_OUTPUT_HANDLE ); + if( handleStdOut != INVALID_HANDLE_VALUE ) + { + freopen( "CONOUT$", "w", stdout ); + setvbuf( stdout, NULL, _IONBF, 0 ); + } + + handleStdErr = GetStdHandle( STD_ERROR_HANDLE ); + if( handleStdErr != INVALID_HANDLE_VALUE ) + { + freopen( "CONOUT$", "w", stderr ); + setvbuf( stderr, NULL, _IONBF, 0 ); + } + } + return true; }