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

Pick-to: 6.6
Change-Id: I1c5033cc962dcd5a757ff15cc677a51e21330e0b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Zhao Yuhang 2023-10-22 16:05:25 +08:00 committed by Yuhang Zhao
parent 6a93ec2435
commit fd300f143f

View File

@ -580,7 +580,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.