From e07456f4bf2135bcb78cbd5ae1f10d1bbe45490c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Thu, 9 Jun 2016 01:58:50 +0200 Subject: [PATCH] GH-1586 Windows: attach to the parent process console if there is any, so command line output gets printed there --- application/MultiMC.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ application/MultiMC.h | 2 ++ 2 files changed, 43 insertions(+) diff --git a/application/MultiMC.cpp b/application/MultiMC.cpp index c9c7d41b..7b891215 100644 --- a/application/MultiMC.cpp +++ b/application/MultiMC.cpp @@ -52,10 +52,42 @@ #include #include +#if defined Q_OS_WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#include +#endif + using namespace Commandline; MultiMC::MultiMC(int &argc, char **argv, bool test_mode) : QApplication(argc, argv) { +#if defined Q_OS_WIN32 + // attach the parent console + if(AttachConsole(ATTACH_PARENT_PROCESS)) + { + // if attach succeeds, reopen and sync all the i/o + if(freopen("CON", "w", stdout)) + { + std::cout.sync_with_stdio(); + } + if(freopen("CON", "w", stderr)) + { + std::cerr.sync_with_stdio(); + } + if(freopen("CON", "r", stdin)) + { + std::cin.sync_with_stdio(); + } + auto out = GetStdHandle (STD_OUTPUT_HANDLE); + DWORD written; + const char * endline = "\n"; + WriteConsole(out, endline, strlen(endline), &written, NULL); + consoleAttached = true; + } +#endif setOrganizationName("MultiMC"); setApplicationName("MultiMC5"); @@ -279,6 +311,15 @@ MultiMC::~MultiMC() { removeTranslator(m_qt_translator.get()); } +#if defined Q_OS_WIN32 + if(consoleAttached) + { + const char * endline = "\n"; + auto out = GetStdHandle (STD_OUTPUT_HANDLE); + DWORD written; + WriteConsole(out, endline, strlen(endline), &written, NULL); + } +#endif } #ifdef Q_OS_MAC diff --git a/application/MultiMC.h b/application/MultiMC.h index 8219d66c..887f9c2d 100644 --- a/application/MultiMC.h +++ b/application/MultiMC.h @@ -176,6 +176,8 @@ private: QString rootPath; Status m_status = MultiMC::Failed; + + bool consoleAttached = false; public: QString launchId; std::shared_ptr logFile;