From bd4e035dacd11be2750c655645292fae9d7ea09f Mon Sep 17 00:00:00 2001 From: Julian Noble Date: Fri, 29 Aug 2025 02:39:01 +1000 Subject: [PATCH] getpunk windows SetConsoleMode take3 --- getpunk.cmd | 15 +++++++++++---- src/scriptapps/getpunk.ps1 | 15 +++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/getpunk.cmd b/getpunk.cmd index 8a00f706..074ab0ea 100644 --- a/getpunk.cmd +++ b/getpunk.cmd @@ -1028,6 +1028,7 @@ git checkout "${scriptroot}.cmd" git branch --set-upstream-to=origin/master master Set-Location $launchdir #restore original CWD +#see also: https://github.com/jdhitsolutions/WTToolbox # Define the necessary Win32 API functions and constants Add-Type -TypeDefinition @" @@ -1038,6 +1039,14 @@ public class WinAPI { // Console Input/Output Handles public const int STD_OUTPUT_HANDLE = -11; + public const uint ENABLE_QUICK_EDIT_MODE = 0x0040; + public const uint ENABLE_EXTENDED_FLAGS = 0x0080; + public const uint ENABLE_MOUSE_INPUT = 0x0010; + public const uint ENABLE_WINDOW_INPUT = 0x0008; + public const uint ENABLE_INSERT_MODE = 0x0020; + public const uint ENABLE_LINE_INPUT = 0x0002; + public const uint ENABLE_ECHO_INPUT = 0x0004; + public const uint ENABLE_PROCESSED_INPUT = 0x0001; // Console Modes public const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; @@ -1045,10 +1054,8 @@ public class WinAPI [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr GetStdHandle(int nStdHandle); - [DllImport("kernel32.dll", SetLastError = true)] public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); - [DllImport("kernel32.dll", SetLastError = true)] public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); } @@ -1058,7 +1065,7 @@ public class WinAPI $stdoutHandle = [WinAPI]::GetStdHandle([WinAPI]::STD_OUTPUT_HANDLE) # Get the current console mode -[uint]$currentMode = 0 +[uint32]$currentMode = 0 if (![WinAPI]::GetConsoleMode($stdoutHandle, [ref]$currentMode)) { Write-Error "Failed to get console mode. Error code: $($LAST_ERROR)" return @@ -1068,7 +1075,7 @@ if (![WinAPI]::GetConsoleMode($stdoutHandle, [ref]$currentMode)) { $newMode = $currentMode -bor [WinAPI]::ENABLE_VIRTUAL_TERMINAL_PROCESSING # Set the new console mode -if (![WinAPI]::SetConsoleMode($stdoutHandle, $newMode)) { +if (-not [WinAPI]::SetConsoleMode($stdoutHandle, $newMode)) { Write-Error "Failed to set console mode. Error code: $($LAST_ERROR)" return } diff --git a/src/scriptapps/getpunk.ps1 b/src/scriptapps/getpunk.ps1 index 53ec857d..8393ffff 100644 --- a/src/scriptapps/getpunk.ps1 +++ b/src/scriptapps/getpunk.ps1 @@ -119,6 +119,7 @@ git checkout "${scriptroot}.cmd" git branch --set-upstream-to=origin/master master Set-Location $launchdir #restore original CWD +#see also: https://github.com/jdhitsolutions/WTToolbox # Define the necessary Win32 API functions and constants Add-Type -TypeDefinition @" @@ -129,6 +130,14 @@ public class WinAPI { // Console Input/Output Handles public const int STD_OUTPUT_HANDLE = -11; + public const uint ENABLE_QUICK_EDIT_MODE = 0x0040; + public const uint ENABLE_EXTENDED_FLAGS = 0x0080; + public const uint ENABLE_MOUSE_INPUT = 0x0010; + public const uint ENABLE_WINDOW_INPUT = 0x0008; + public const uint ENABLE_INSERT_MODE = 0x0020; + public const uint ENABLE_LINE_INPUT = 0x0002; + public const uint ENABLE_ECHO_INPUT = 0x0004; + public const uint ENABLE_PROCESSED_INPUT = 0x0001; // Console Modes public const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; @@ -136,10 +145,8 @@ public class WinAPI [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr GetStdHandle(int nStdHandle); - [DllImport("kernel32.dll", SetLastError = true)] public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); - [DllImport("kernel32.dll", SetLastError = true)] public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); } @@ -149,7 +156,7 @@ public class WinAPI $stdoutHandle = [WinAPI]::GetStdHandle([WinAPI]::STD_OUTPUT_HANDLE) # Get the current console mode -[uint]$currentMode = 0 +[uint32]$currentMode = 0 if (![WinAPI]::GetConsoleMode($stdoutHandle, [ref]$currentMode)) { Write-Error "Failed to get console mode. Error code: $($LAST_ERROR)" return @@ -159,7 +166,7 @@ if (![WinAPI]::GetConsoleMode($stdoutHandle, [ref]$currentMode)) { $newMode = $currentMode -bor [WinAPI]::ENABLE_VIRTUAL_TERMINAL_PROCESSING # Set the new console mode -if (![WinAPI]::SetConsoleMode($stdoutHandle, $newMode)) { +if (-not [WinAPI]::SetConsoleMode($stdoutHandle, $newMode)) { Write-Error "Failed to set console mode. Error code: $($LAST_ERROR)" return }