Browse Source

getpunk windows SetConsoleMode take3

master
Julian Noble 4 days ago
parent
commit
bd4e035dac
  1. 15
      getpunk.cmd
  2. 15
      src/scriptapps/getpunk.ps1

15
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
}

15
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
}

Loading…
Cancel
Save