GH-1586 Windows: attach to the parent process console if there is any, so command line output gets printed there

This commit is contained in:
Petr Mrázek 2016-06-09 01:58:50 +02:00
parent 54e5a98da0
commit e07456f4bf
2 changed files with 43 additions and 0 deletions

View File

@ -52,10 +52,42 @@
#include <FileSystem.h>
#include <DesktopServices.h>
#if defined Q_OS_WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <stdio.h>
#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

View File

@ -176,6 +176,8 @@ private:
QString rootPath;
Status m_status = MultiMC::Failed;
bool consoleAttached = false;
public:
QString launchId;
std::shared_ptr<QFile> logFile;