From a8e601ae7d8f858db3180edf3dacf165c823644b Mon Sep 17 00:00:00 2001
From: PabloMK7 <hackyglitch2@gmail.com>
Date: Mon, 22 Apr 2024 23:37:59 +0200
Subject: [PATCH] Fix showing color console if debugger attached on windows.
 (#88)

---
 src/citra_qt/debugger/console.cpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/citra_qt/debugger/console.cpp b/src/citra_qt/debugger/console.cpp
index 8187b0b1d..ed7a1cc85 100644
--- a/src/citra_qt/debugger/console.cpp
+++ b/src/citra_qt/debugger/console.cpp
@@ -25,7 +25,14 @@ void ToggleConsole() {
 #ifdef _WIN32
     FILE* temp;
     if (UISettings::values.show_console) {
-        if (AllocConsole()) {
+        BOOL alloc_console_res = AllocConsole();
+        DWORD last_error = 0;
+        if (!alloc_console_res) {
+            last_error = GetLastError();
+        }
+        // If the windows debugger already opened a console, calling AllocConsole again
+        // will cause ERROR_ACCESS_DENIED. If that's the case assume a console is open.
+        if (alloc_console_res || last_error == ERROR_ACCESS_DENIED) {
             // The first parameter for freopen_s is a out parameter, so we can just ignore it
             freopen_s(&temp, "CONIN$", "r", stdin);
             freopen_s(&temp, "CONOUT$", "w", stdout);