From 502fb0c554f3bf0f75ea649898f5ded46947d396 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Wed, 28 Feb 2024 11:26:15 +0100 Subject: [PATCH] Windows: issue a warning for PWSH/cmd term only in W10 Do not issue the warning over PowerShell or cmd legacy terminal emulators in Windows 11. In Window the check doesn't work, because the process tree is different - the Windows Terminal doesn't have its own process and it is uv.exe->powershell.exe->exporer.exe. This improves commit bb2a72f67f. --- src/host.cpp | 7 ++++--- src/utils/windows.c | 32 ++++++++++++++++++++++++++++++++ src/utils/windows.h | 5 +++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/host.cpp b/src/host.cpp index 4e13b0dd3..6a3f13f1c 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -462,10 +462,11 @@ struct init_data *common_preinit(int argc, char *argv[]) } // warn in W10 "legacy" terminal emulators - if ((win_has_ancestor_process("powershell.exe") || + if (getenv("TERM") == nullptr && + get_windows_build() < BUILD_WINDOWS_11_OR_LATER && + (win_has_ancestor_process("powershell.exe") || win_has_ancestor_process("cmd.exe")) && - !win_has_ancestor_process("WindowsTerminal.exe") && - getenv("TERM") == nullptr) { + !win_has_ancestor_process("WindowsTerminal.exe")) { MSG(WARNING, "Running inside PS/cmd terminal is not recommended " "because scrolling the output freezes the process, " "consider using Windows Terminal instead!\n"); diff --git a/src/utils/windows.c b/src/utils/windows.c index 08c77de54..72ac53030 100644 --- a/src/utils/windows.c +++ b/src/utils/windows.c @@ -37,6 +37,7 @@ #ifdef _WIN32 #include +#include #include #include #include @@ -239,6 +240,37 @@ win_has_ancestor_process(const char *name) return false; } +unsigned long +get_windows_build() +{ + RTL_OSVERSIONINFOW osVersionInfo = { .dwOSVersionInfoSize = + sizeof(RTL_OSVERSIONINFOW) }; + + HMODULE hNtDll = GetModuleHandleW(L"ntdll.dll"); + if (hNtDll == NULL) { + MSG(VERBOSE, "Cannot load ntdll.dll!\n"); + return 0; + } + + typedef NTSTATUS(WINAPI * RtlGetVersionFunc)( + PRTL_OSVERSIONINFOW lpVersionInformation); + RtlGetVersionFunc pRtlGetVersion = + (RtlGetVersionFunc) GetProcAddress(hNtDll, "RtlGetVersion"); + if (pRtlGetVersion == NULL) { + MSG(VERBOSE, "Cannot get RtlGetVersion from ntdll.dll!\n"); + return 0; + } + + if (pRtlGetVersion(&osVersionInfo) != STATUS_SUCCESS) { + MSG(VERBOSE, "Cannot get Windows version nfo!\n"); + return 0; + } + MSG(DEBUG, "Windows version: %lu.%lu (build %lu)\n", + osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion, + osVersionInfo.dwBuildNumber); + return osVersionInfo.dwBuildNumber; +} + #endif // defined _WIN32 /* vim: set expandtab sw=8 tw=120: */ diff --git a/src/utils/windows.h b/src/utils/windows.h index 41c3859c3..1d6aead8a 100644 --- a/src/utils/windows.h +++ b/src/utils/windows.h @@ -42,6 +42,10 @@ #include #endif +enum { + BUILD_WINDOWS_11_OR_LATER = 22000, +}; + #ifdef __cplusplus extern "C" { #endif @@ -86,6 +90,7 @@ const char *hresult_to_str(HRESULT res); const char *get_win32_error(DWORD error); const char *win_wstr_to_str(const wchar_t *wstr); bool win_has_ancestor_process(const char *name); +unsigned long get_windows_build(void); #endif // defined _WIN32 #ifdef __cplusplus