diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 04c74748032..d9a42413e4d 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -451,6 +451,8 @@ QCoreApplicationPrivate::~QCoreApplicationPrivate() #endif #if defined(Q_OS_WIN) delete [] origArgv; + if (consoleAllocated) + FreeConsole(); #endif QCoreApplicationPrivate::clearApplicationFilePath(); } @@ -549,6 +551,37 @@ QString qAppName() return QCoreApplication::instance()->d_func()->appName(); } +void QCoreApplicationPrivate::initConsole() +{ +#ifdef Q_OS_WINDOWS + const QString env = qEnvironmentVariable("QT_WIN_DEBUG_CONSOLE"); + if (env.isEmpty()) + return; + if (env.compare(u"new"_s, Qt::CaseInsensitive) == 0) { + if (AllocConsole() == FALSE) + return; + consoleAllocated = true; + } else if (env.compare(u"attach"_s, Qt::CaseInsensitive) == 0) { + if (AttachConsole(ATTACH_PARENT_PROCESS) == FALSE) + return; + } else { + // Unknown input, don't make any decision for the user. + return; + } + // The std{in,out,err} handles are read-only, so we need to pass in dummies. + FILE *in = nullptr; + FILE *out = nullptr; + FILE *err = nullptr; + freopen_s(&in, "CONIN$", "r", stdin); + freopen_s(&out, "CONOUT$", "w", stdout); + freopen_s(&err, "CONOUT$", "w", stderr); + // However, things wouldn't work if the runtime did not preserve the pointers. + Q_ASSERT(in == stdin); + Q_ASSERT(out == stdout); + Q_ASSERT(err == stderr); +#endif +} + void QCoreApplicationPrivate::initLocale() { #if defined(Q_OS_UNIX) && !defined(QT_BOOTSTRAPPED) @@ -744,6 +777,8 @@ void QCoreApplicationPrivate::init() Q_Q(QCoreApplication); + initConsole(); + initLocale(); Q_ASSERT_X(!QCoreApplication::self, "QCoreApplication", "there should be only one application object"); diff --git a/src/corelib/kernel/qcoreapplication_p.h b/src/corelib/kernel/qcoreapplication_p.h index 94c4b0f1e98..56d726cff59 100644 --- a/src/corelib/kernel/qcoreapplication_p.h +++ b/src/corelib/kernel/qcoreapplication_p.h @@ -73,6 +73,7 @@ public: static QString infoDictionaryStringProperty(const QString &propertyName); #endif + void initConsole(); static void initLocale(); static bool checkInstance(const char *method); @@ -125,6 +126,7 @@ public: #if defined(Q_OS_WIN) int origArgc; char **origArgv; // store unmodified arguments for QCoreApplication::arguments() + bool consoleAllocated = false; #endif void appendApplicationPathToLibraryPaths(void);