Win: minor improvement of the console debug trick

If the application has already attached to the console, the
AttachConsole() API will fail but we can still redirect our
output to the console. The previous code doesn't take this
case into consideration, now it's covered by this patch.

Please refer to the "Remarks" section:
https://learn.microsoft.com/en-us/windows/console/attachconsole

Change-Id: I1c5033cc962dcd5a757ff15cc677a51e21330e0b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit fd300f143fd30947bba60a03d614acd2711b635f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Zhao Yuhang 2023-10-22 16:05:25 +08:00 committed by Qt Cherry-pick Bot
parent b80262055c
commit 9f971deb06

View File

@ -577,7 +577,9 @@ void QCoreApplicationPrivate::initConsole()
return;
consoleAllocated = true;
} else if (env.compare(u"attach"_s, Qt::CaseInsensitive) == 0) {
if (AttachConsole(ATTACH_PARENT_PROCESS) == FALSE)
// If the calling process is already attached to a console,
// the error code returned is ERROR_ACCESS_DENIED.
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::GetLastError() != ERROR_ACCESS_DENIED)
return;
} else {
// Unknown input, don't make any decision for the user.