From 86cd212d7f4946543777a346fe26fc1b2f207f13 Mon Sep 17 00:00:00 2001 From: Rachael Alexanderson Date: Wed, 16 Oct 2024 05:57:58 -0400 Subject: [PATCH] - fix isolated console mode (i.e. launching from explorer.exe) --- src/CMakeLists.txt | 2 +- src/common/platform/win32/i_main.cpp | 43 ++++++++++++++++---------- src/common/platform/win32/i_system.cpp | 2 ++ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 987f53dc0f..95b67a2d87 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1361,7 +1361,7 @@ endif() if( MSVC ) option ( CONSOLE_MODE "Compile as a console application" OFF ) if ( CONSOLE_MODE ) - set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE /ENTRY:wWinMainCRTStartup" ) + set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE /ENTRY:wmainCRTStartup" ) else() set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS /ENTRY:wWinMainCRTStartup" ) endif() diff --git a/src/common/platform/win32/i_main.cpp b/src/common/platform/win32/i_main.cpp index 64c74e8080..aa1f6271e3 100644 --- a/src/common/platform/win32/i_main.cpp +++ b/src/common/platform/win32/i_main.cpp @@ -45,6 +45,11 @@ #include #include +#include +#include +#include +#include + #ifdef _MSC_VER #pragma warning(disable:4244) #endif @@ -149,11 +154,22 @@ void I_SetIWADInfo() bool isConsoleApp() { - DWORD pids[2]; - DWORD num_pids = GetConsoleProcessList(pids, 2); - bool win32con_is_exclusive = (num_pids <= 1); + static bool alreadychecked = false; + static bool returnvalue; - return GetConsoleWindow() != NULL && !win32con_is_exclusive; + if (!alreadychecked) + { + DWORD pids[2]; + DWORD num_pids = GetConsoleProcessList(pids, 2); + bool win32con_is_exclusive = (num_pids <= 1); + + returnvalue = ((GetConsoleWindow() != NULL && !win32con_is_exclusive) || (GetStdHandle(STD_OUTPUT_HANDLE) != NULL)); + alreadychecked = true; + } + + //printf("isConsoleApp is %i\n", returnvalue); + + return returnvalue; } //========================================================================== @@ -183,19 +199,7 @@ int DoMain (HINSTANCE hInstance) if (isConsoleApp()) { StdOut = GetStdHandle(STD_OUTPUT_HANDLE); - BY_HANDLE_FILE_INFORMATION info; - - if (!GetFileInformationByHandle(StdOut, &info) && StdOut != nullptr) - { - SetConsoleCP(CP_UTF8); - SetConsoleOutputCP(CP_UTF8); - DWORD mode; - if (GetConsoleMode(StdOut, &mode)) - { - if (SetConsoleMode(StdOut, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) - FancyStdOut = IsWindows10OrGreater(); // Windows 8.1 and lower do not understand ANSI formatting. - } - } + FancyStdOut = IsWindows10OrGreater(); // Windows 8.1 and lower do not understand ANSI formatting. } else if (Args->CheckParm("-stdout") || Args->CheckParm("-norun")) { @@ -514,6 +518,11 @@ CUSTOM_CVAR(Bool, disablecrashlog, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) // //========================================================================== +int wmain() +{ + return wWinMain(GetModuleHandle(0), 0, GetCommandLineW(), SW_SHOW); +} + int WINAPI wWinMain (HINSTANCE hInstance, HINSTANCE nothing, LPWSTR cmdline, int nCmdShow) { g_hInst = hInstance; diff --git a/src/common/platform/win32/i_system.cpp b/src/common/platform/win32/i_system.cpp index 71a4ded122..caed820b0c 100644 --- a/src/common/platform/win32/i_system.cpp +++ b/src/common/platform/win32/i_system.cpp @@ -102,6 +102,7 @@ // PUBLIC FUNCTION PROTOTYPES ---------------------------------------------- void DestroyCustomCursor(); +bool isConsoleApp(); // PRIVATE FUNCTION PROTOTYPES --------------------------------------------- @@ -306,6 +307,7 @@ static void PrintToStdOut(const char *cpt, HANDLE StdOut) else break; } } + DWORD bytes_written; WriteFile(StdOut, printData.GetChars(), (DWORD)printData.Len(), &bytes_written, NULL); if (terminal)