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
| using Microsoft.Terminal.Wpf; using System; using System.IO; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks;
public class ConPtyConnection : ITerminalConnection, IDisposable { private readonly string _commandLine; private uint _cols; private uint _rows;
public ConPtyConnection(string commandLine = "cmd.exe", uint cols = 120, uint rows = 30) { _commandLine = commandLine; _cols = cols; _rows = rows; }
[DllImport("kernel32.dll", SetLastError = true)] static extern bool CreatePipe(out IntPtr hRead, out IntPtr hWrite, ref SECURITY_ATTRIBUTES lpPipeAttributes, int nSize);
[DllImport("kernel32.dll", SetLastError = true)] static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true)] static extern int CreatePseudoConsole(COORD size, IntPtr hInput, IntPtr hOutput, uint dwFlags, out IntPtr phPC);
[DllImport("kernel32.dll", SetLastError = true)] static extern int ResizePseudoConsole(IntPtr hPC, COORD size);
[DllImport("kernel32.dll", SetLastError = true)] static extern void ClosePseudoConsole(IntPtr hPC);
[DllImport("kernel32.dll", SetLastError = true)] static extern bool InitializeProcThreadAttributeList( IntPtr lpAttributeList, int dwAttributeCount, int dwFlags, ref IntPtr lpSize);
[DllImport("kernel32.dll", SetLastError = true)] static extern bool UpdateProcThreadAttribute( IntPtr lpAttributeList, uint dwFlags, IntPtr Attribute, IntPtr lpValue, IntPtr cbSize, IntPtr lpPreviousValue, IntPtr lpReturnSize);
[DllImport("kernel32.dll", SetLastError = true)] static extern bool DeleteProcThreadAttributeList(IntPtr lpAttributeList);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] static extern bool CreateProcess( string lpApplicationName, string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFOEX lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
[DllImport("kernel32.dll", SetLastError = true)] static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)] static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer, int nNumberOfBytesToRead, out int lpNumberOfBytesRead, IntPtr lpOverlapped);
[StructLayout(LayoutKind.Sequential)] struct COORD { public short X, Y; }
[StructLayout(LayoutKind.Sequential)] struct SECURITY_ATTRIBUTES { public int nLength; public IntPtr lpSecurityDescriptor; public bool bInheritHandle; }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] struct STARTUPINFO { public int cb; public string lpReserved, lpDesktop, lpTitle; public int dwX, dwY, dwXSize, dwYSize; public int dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags; public short wShowWindow, cbReserved2; public IntPtr lpReserved2, hStdInput, hStdOutput, hStdError; }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] struct STARTUPINFOEX { public STARTUPINFO StartupInfo; public IntPtr lpAttributeList; }
[StructLayout(LayoutKind.Sequential)] struct PROCESS_INFORMATION { public IntPtr hProcess, hThread; public int dwProcessId, dwThreadId; }
const uint EXTENDED_STARTUPINFO_PRESENT = 0x00080000; static readonly IntPtr PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE = new IntPtr(0x00020016);
public event EventHandler<TerminalOutputEventArgs> TerminalOutput;
IntPtr _hPC = IntPtr.Zero; IntPtr _hPipeIn = IntPtr.Zero; IntPtr _hPipeOut = IntPtr.Zero; IntPtr _hPipeInRead = IntPtr.Zero; IntPtr _hPipeOutWrite = IntPtr.Zero; PROCESS_INFORMATION _pi; CancellationTokenSource _cts = new();
public void Start() { var sa = new SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf<SECURITY_ATTRIBUTES>(), bInheritHandle = true };
CreatePipe(out _hPipeInRead, out _hPipeIn, ref sa, 0); CreatePipe(out _hPipeOut, out _hPipeOutWrite, ref sa, 0);
var size = new COORD { X = (short)_cols, Y = (short)_rows }; CreatePseudoConsole(size, _hPipeInRead, _hPipeOutWrite, 0, out _hPC);
var siEx = new STARTUPINFOEX(); siEx.StartupInfo.cb = Marshal.SizeOf<STARTUPINFOEX>(); IntPtr size2 = IntPtr.Zero; InitializeProcThreadAttributeList(IntPtr.Zero, 1, 0, ref size2); siEx.lpAttributeList = Marshal.AllocHGlobal(size2); InitializeProcThreadAttributeList(siEx.lpAttributeList, 1, 0, ref size2); UpdateProcThreadAttribute(siEx.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, _hPC, new IntPtr(IntPtr.Size), IntPtr.Zero, IntPtr.Zero);
var psa = new SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf<SECURITY_ATTRIBUTES>() }; var tsa = new SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf<SECURITY_ATTRIBUTES>() };
CreateProcess(null, _commandLine, ref psa, ref tsa, false, EXTENDED_STARTUPINFO_PRESENT, IntPtr.Zero, null, ref siEx, out _pi);
DeleteProcThreadAttributeList(siEx.lpAttributeList); Marshal.FreeHGlobal(siEx.lpAttributeList);
Task.Run(() => ReadLoop(_cts.Token)); }
void ReadLoop(CancellationToken ct) { var buf = new byte[4096]; while (!ct.IsCancellationRequested) { if (!ReadFile(_hPipeOut, buf, buf.Length, out int bytesRead, IntPtr.Zero) || bytesRead == 0) break; var text = System.Text.Encoding.UTF8.GetString(buf, 0, bytesRead); TerminalOutput?.Invoke(this, new TerminalOutputEventArgs(text)); } }
public void WriteInput(string data) { if (_hPipeIn == IntPtr.Zero) return; var bytes = System.Text.Encoding.UTF8.GetBytes(data); WriteFile(_hPipeIn, bytes, bytes.Length, out _, IntPtr.Zero); }
public void Resize(uint rows, uint columns) { _cols = columns; _rows = rows; if (_hPC != IntPtr.Zero) ResizePseudoConsole(_hPC, new COORD { X = (short)columns, Y = (short)rows }); }
public void Close() { _cts.Cancel(); if (_hPC != IntPtr.Zero) { ClosePseudoConsole(_hPC); _hPC = IntPtr.Zero; } if (_hPipeIn != IntPtr.Zero) { CloseHandle(_hPipeIn); _hPipeIn = IntPtr.Zero; } if (_hPipeOut != IntPtr.Zero) { CloseHandle(_hPipeOut); _hPipeOut = IntPtr.Zero; } if (_hPipeInRead != IntPtr.Zero) { CloseHandle(_hPipeInRead); _hPipeInRead = IntPtr.Zero; } if (_hPipeOutWrite != IntPtr.Zero) { CloseHandle(_hPipeOutWrite); _hPipeOutWrite = IntPtr.Zero; } }
public void Dispose() => Close(); }
|