-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_host_environment.ps1
More file actions
107 lines (87 loc) · 3 KB
/
detect_host_environment.ps1
File metadata and controls
107 lines (87 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# detecting “pseudo-terminal” execution environment introduced in Windows 11
# where is no real Win32 window to attach modals to
#
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class ConsoleInfo
{
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("kernel32.dll")]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int lpMode);
[DllImport("kernel32.dll")]
public static extern IntPtr GetStdHandle(int nStdHandle);
public static string DetectEnvironment()
{
// Stdout handle
IntPtr handle = GetStdHandle(-11); // STD_OUTPUT_HANDLE
int mode;
bool res = GetConsoleMode(handle, out mode);
Console.Error.WriteLine(String.Format("res: {0}", res));
if (!res) {
// No console mode → must be a pseudoterminal (WSL, mintty, git-bash, ConPTY)
IntPtr hwnd = GetConsoleWindow();
if (hwnd != IntPtr.Zero)
return "ConPTY-backed console (Windows Terminal)";
return "True PTY (mintty / git-bash / WSL) — no Win32 console exists";
}
// We have a Win32 console (conhost.exe)
return "Classic Win32 console (conhost.exe)";
}
}
"@
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public static class ConsoleHelper {
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
// Returns the HWND of the console, or IntPtr.Zero if none exists
public static IntPtr GetConsoleHwnd()
{
return GetConsoleWindow();
}
// Returns a string describing the console environment
public static string DetectConsoleType()
{
IntPtr hwnd = GetConsoleWindow();
if (hwnd != IntPtr.Zero)
return "Classic Win32 console (conhost.exe)";
return "ConPTY-backed console (Windows Terminal) — no HWND available";
}
}
"@ -ReferencedAssemblies 'System.Windows.Forms.dll'
# non-working
[ConsoleInfo]::DetectEnvironment()
$hwnd = [ConsoleHelper]::GetConsoleHwnd()
$consoleType = [ConsoleHelper]::DetectConsoleType()
Write-Host "Console type: $consoleType"
Write-Host "HWND: $hwnd"
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public static class ConsoleInfo2 {
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
}
"@
$hwnd = [ConsoleInfo2]::GetConsoleWindow()
$process = if ($hwnd -ne [IntPtr]::Zero) {
# write-host ('looking for process with MainWindowHandle {0}' -f $hwnd )
$pid3 = (Get-Process | Where-Object { $_.MainWindowHandle -eq $hwnd }).Id
if ($pid3 -ne $null ) {
Get-Process -Id $pid3
} else { $null }
} else { $null }
if ($process -eq $null) {
Write-Host "No Win32 console window found - probably Windows Terminal / ConPTY."
$isClassicConsole = $false
} elseif ($process.ProcessName -eq "conhost") {
Write-Host "Classic Win32 console detected."
$isClassicConsole = $true
} else {
Write-Host "Unknown console host: $($process.ProcessName)"
$isClassicConsole = $false
}