Skip to content

Commit 8f3d3cc

Browse files
committed
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 bb2a72f.
1 parent c507f5c commit 8f3d3cc

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/host.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,11 @@ struct init_data *common_preinit(int argc, char *argv[])
462462
}
463463

464464
// warn in W10 "legacy" terminal emulators
465-
if ((win_has_ancestor_process("powershell.exe") ||
465+
if (getenv("TERM") == nullptr &&
466+
get_windows_build() < BUILD_WINDOWS_11_OR_LATER &&
467+
(win_has_ancestor_process("powershell.exe") ||
466468
win_has_ancestor_process("cmd.exe")) &&
467-
!win_has_ancestor_process("WindowsTerminal.exe") &&
468-
getenv("TERM") == nullptr) {
469+
!win_has_ancestor_process("WindowsTerminal.exe")) {
469470
MSG(WARNING, "Running inside PS/cmd terminal is not recommended "
470471
"because scrolling the output freezes the process, "
471472
"consider using Windows Terminal instead!\n");

src/utils/windows.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#ifdef _WIN32
3939
#include <audioclient.h>
40+
#include <ntstatus.h>
4041
#include <objbase.h>
4142
#include <tlhelp32.h>
4243
#include <vfwmsgs.h>
@@ -239,6 +240,37 @@ win_has_ancestor_process(const char *name)
239240
return false;
240241
}
241242

243+
unsigned long
244+
get_windows_build()
245+
{
246+
RTL_OSVERSIONINFOW osVersionInfo = { .dwOSVersionInfoSize =
247+
sizeof(RTL_OSVERSIONINFOW) };
248+
249+
HMODULE hNtDll = GetModuleHandleW(L"ntdll.dll");
250+
if (hNtDll == NULL) {
251+
MSG(VERBOSE, "Cannot load ntdll.dll!\n");
252+
return 0;
253+
}
254+
255+
typedef NTSTATUS(WINAPI * RtlGetVersionFunc)(
256+
PRTL_OSVERSIONINFOW lpVersionInformation);
257+
RtlGetVersionFunc pRtlGetVersion =
258+
(RtlGetVersionFunc) GetProcAddress(hNtDll, "RtlGetVersion");
259+
if (pRtlGetVersion == NULL) {
260+
MSG(VERBOSE, "Cannot get RtlGetVersion from ntdll.dll!\n");
261+
return 0;
262+
}
263+
264+
if (pRtlGetVersion(&osVersionInfo) != STATUS_SUCCESS) {
265+
MSG(VERBOSE, "Cannot get Windows version nfo!\n");
266+
return 0;
267+
}
268+
MSG(DEBUG, "Windows version: %lu.%lu (build %lu)\n",
269+
osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion,
270+
osVersionInfo.dwBuildNumber);
271+
return osVersionInfo.dwBuildNumber;
272+
}
273+
242274
#endif // defined _WIN32
243275

244276
/* vim: set expandtab sw=8 tw=120: */

src/utils/windows.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
#include <stdbool.h>
4343
#endif
4444

45+
enum {
46+
BUILD_WINDOWS_11_OR_LATER = 22000,
47+
};
48+
4549
#ifdef __cplusplus
4650
extern "C" {
4751
#endif
@@ -86,6 +90,7 @@ const char *hresult_to_str(HRESULT res);
8690
const char *get_win32_error(DWORD error);
8791
const char *win_wstr_to_str(const wchar_t *wstr);
8892
bool win_has_ancestor_process(const char *name);
93+
unsigned long get_windows_build(void);
8994
#endif // defined _WIN32
9095

9196
#ifdef __cplusplus

0 commit comments

Comments
 (0)