-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConPtyProcess.cs
More file actions
313 lines (261 loc) · 10.1 KB
/
ConPtyProcess.cs
File metadata and controls
313 lines (261 loc) · 10.1 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#if PLATFORM_WINDOWS
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Win32.SafeHandles;
namespace ExcelConsole;
/// <summary>
/// Runs a command inside a Windows Pseudo Console (ConPTY), capturing all output
/// including from programs that use Console APIs directly (TUI/interactive apps).
/// </summary>
internal sealed class ConPtyProcess : IDisposable
{
#region P/Invoke
[StructLayout(LayoutKind.Sequential)]
private struct COORD { public short X, Y; }
[StructLayout(LayoutKind.Sequential)]
private struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
[MarshalAs(UnmanagedType.Bool)] public bool bInheritHandle;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct STARTUPINFOEX
{
public STARTUPINFO StartupInfo;
public IntPtr lpAttributeList;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct STARTUPINFO
{
public int cb;
public IntPtr lpReserved;
public IntPtr lpDesktop;
public IntPtr lpTitle;
public int dwX, dwY, dwXSize, dwYSize;
public int dwXCountChars, dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput, hStdOutput, hStdError;
}
[StructLayout(LayoutKind.Sequential)]
private struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
private const uint EXTENDED_STARTUPINFO_PRESENT = 0x00080000;
private static readonly IntPtr PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE = (IntPtr)0x00020016;
private const uint STILL_ACTIVE = 259;
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CreatePipe(out IntPtr hReadPipe, out IntPtr hWritePipe,
ref SECURITY_ATTRIBUTES sa, int nSize);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern int CreatePseudoConsole(COORD size, IntPtr hInput, IntPtr hOutput,
uint dwFlags, out IntPtr phPC);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern void ClosePseudoConsole(IntPtr hPC);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool InitializeProcThreadAttributeList(IntPtr lpAttributeList,
int dwAttributeCount, int dwFlags, ref IntPtr lpSize);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool UpdateProcThreadAttribute(IntPtr lpAttributeList, uint dwFlags,
IntPtr Attribute, IntPtr lpValue, IntPtr cbSize, IntPtr lpPreviousValue, IntPtr lpReturnSize);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool DeleteProcThreadAttributeList(IntPtr lpAttributeList);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool CreateProcessW(string? lpApplicationName, string lpCommandLine,
IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,
uint dwCreationFlags, IntPtr lpEnvironment, string? lpCurrentDirectory,
ref STARTUPINFOEX lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
private static extern bool GetExitCodeProcess(IntPtr hProcess, out uint lpExitCode);
#endregion
private const int MaxLines = 200;
private IntPtr _hPC;
private IntPtr _hProcess;
private IntPtr _hThread;
private IntPtr _outputReadPipe;
private IntPtr _inputWritePipe;
private IntPtr _attrList;
private Thread? _readerThread;
private volatile bool _disposed;
private readonly object _lock = new();
private readonly List<string> _outputLines = new();
public bool HasNewOutput { get; set; }
public bool HasExited
{
get
{
if (_hProcess == IntPtr.Zero) return true;
GetExitCodeProcess(_hProcess, out uint code);
return code != STILL_ACTIVE;
}
}
/// <summary>
/// Starts the command in a pseudo console. Returns true on success.
/// </summary>
public bool Start(string command, int cols = 120, int rows = 30)
{
var sa = new SECURITY_ATTRIBUTES
{
nLength = Marshal.SizeOf<SECURITY_ATTRIBUTES>(),
bInheritHandle = true
};
// Output pipe: we read, pseudo console writes
if (!CreatePipe(out var outRead, out var outWrite, ref sa, 0))
return false;
// Input pipe: we write, pseudo console reads
if (!CreatePipe(out var inRead, out var inWrite, ref sa, 0))
{
CloseHandle(outRead);
CloseHandle(outWrite);
return false;
}
_outputReadPipe = outRead;
_inputWritePipe = inWrite;
var size = new COORD { X = (short)cols, Y = (short)rows };
int hr = CreatePseudoConsole(size, inRead, outWrite, 0, out _hPC);
// These ends now belong to the pseudo console
CloseHandle(inRead);
CloseHandle(outWrite);
if (hr != 0)
{
Cleanup();
return false;
}
// Attribute list with pseudo console handle
IntPtr attrSize = IntPtr.Zero;
InitializeProcThreadAttributeList(IntPtr.Zero, 1, 0, ref attrSize);
_attrList = Marshal.AllocHGlobal(attrSize);
if (!InitializeProcThreadAttributeList(_attrList, 1, 0, ref attrSize))
{
Cleanup();
return false;
}
if (!UpdateProcThreadAttribute(_attrList, 0, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
_hPC, (IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero))
{
Cleanup();
return false;
}
var si = new STARTUPINFOEX();
si.StartupInfo.cb = Marshal.SizeOf<STARTUPINFOEX>();
si.lpAttributeList = _attrList;
string cmdLine = $"cmd.exe /c {command}";
if (!CreateProcessW(null, cmdLine, IntPtr.Zero, IntPtr.Zero, false,
EXTENDED_STARTUPINFO_PRESENT, IntPtr.Zero, null, ref si, out var pi))
{
Cleanup();
return false;
}
_hProcess = pi.hProcess;
_hThread = pi.hThread;
_readerThread = new Thread(ReadOutput) { IsBackground = true, Name = "ConPTY-Reader" };
_readerThread.Start();
return true;
}
private void ReadOutput()
{
try
{
using var handle = new SafeFileHandle(_outputReadPipe, ownsHandle: false);
using var stream = new FileStream(handle, FileAccess.Read, 4096, false);
var buffer = new byte[4096];
var lineBuffer = new StringBuilder();
while (!_disposed)
{
int n;
try { n = stream.Read(buffer, 0, buffer.Length); }
catch { break; }
if (n == 0) break;
string text = Encoding.UTF8.GetString(buffer, 0, n);
foreach (char ch in text)
{
if (ch == '\n')
{
string line = StripAnsi(lineBuffer.ToString().TrimEnd('\r'));
if (!string.IsNullOrEmpty(line))
AppendLine(line);
lineBuffer.Clear();
}
else
{
lineBuffer.Append(ch);
}
}
}
if (lineBuffer.Length > 0)
{
string final = StripAnsi(lineBuffer.ToString().TrimEnd('\r'));
if (!string.IsNullOrEmpty(final))
AppendLine(final);
}
}
catch { }
}
private void AppendLine(string line)
{
lock (_lock)
{
_outputLines.Add(line);
while (_outputLines.Count > MaxLines)
_outputLines.RemoveAt(0);
HasNewOutput = true;
}
}
public string? GetOutput()
{
lock (_lock)
{
HasNewOutput = false;
if (_outputLines.Count == 0) return null;
return string.Join("\n", _outputLines);
}
}
// Strips ANSI/VT100 escape sequences and control chars
private static readonly Regex AnsiPattern = new(
@"\x1b(?:\[[0-9;?]*[A-Za-z@~]|\][^\x07]*\x07|\([A-Z0-9]|[A-Z=<>7-9])|[\x00-\x08\x0e-\x1f]",
RegexOptions.Compiled);
private static string StripAnsi(string text) =>
AnsiPattern.Replace(text, "");
private void Cleanup()
{
if (_outputReadPipe != IntPtr.Zero) { CloseHandle(_outputReadPipe); _outputReadPipe = IntPtr.Zero; }
if (_inputWritePipe != IntPtr.Zero) { CloseHandle(_inputWritePipe); _inputWritePipe = IntPtr.Zero; }
if (_hPC != IntPtr.Zero) { ClosePseudoConsole(_hPC); _hPC = IntPtr.Zero; }
if (_attrList != IntPtr.Zero)
{
DeleteProcThreadAttributeList(_attrList);
Marshal.FreeHGlobal(_attrList);
_attrList = IntPtr.Zero;
}
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
// Close pseudo console first — breaks the reader pipe
if (_hPC != IntPtr.Zero) { ClosePseudoConsole(_hPC); _hPC = IntPtr.Zero; }
_readerThread?.Join(2000);
if (_hProcess != IntPtr.Zero) { CloseHandle(_hProcess); _hProcess = IntPtr.Zero; }
if (_hThread != IntPtr.Zero) { CloseHandle(_hThread); _hThread = IntPtr.Zero; }
if (_outputReadPipe != IntPtr.Zero) { CloseHandle(_outputReadPipe); _outputReadPipe = IntPtr.Zero; }
if (_inputWritePipe != IntPtr.Zero) { CloseHandle(_inputWritePipe); _inputWritePipe = IntPtr.Zero; }
if (_attrList != IntPtr.Zero)
{
DeleteProcThreadAttributeList(_attrList);
Marshal.FreeHGlobal(_attrList);
_attrList = IntPtr.Zero;
}
}
}
#endif