diff --git a/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/GUIConsole.ConPTY.csproj b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/GUIConsole.ConPTY.csproj
new file mode 100644
index 0000000000..dbdcea46b6
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/GUIConsole.ConPTY.csproj
@@ -0,0 +1,7 @@
+
+
+
+ netstandard2.0
+
+
+
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Native/ConsoleApi.cs b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Native/ConsoleApi.cs
new file mode 100644
index 0000000000..8c0c153a44
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Native/ConsoleApi.cs
@@ -0,0 +1,23 @@
+using System.Runtime.InteropServices;
+
+namespace GUIConsole.ConPTY.Native
+{
+ ///
+ /// PInvoke signatures for Win32's Console API.
+ ///
+ static class ConsoleApi
+ {
+ [DllImport("kernel32.dll", SetLastError = true)]
+ internal static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
+ internal delegate bool ConsoleEventDelegate(CtrlTypes ctrlType);
+
+ internal enum CtrlTypes : uint
+ {
+ CTRL_C_EVENT = 0,
+ CTRL_BREAK_EVENT,
+ CTRL_CLOSE_EVENT,
+ CTRL_LOGOFF_EVENT = 5,
+ CTRL_SHUTDOWN_EVENT
+ }
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Native/ProcessApi.cs b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Native/ProcessApi.cs
new file mode 100644
index 0000000000..c0e0d016d6
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Native/ProcessApi.cs
@@ -0,0 +1,86 @@
+using System;
+using System.Runtime.InteropServices;
+
+namespace GUIConsole.ConPTY.Native
+{
+ ///
+ /// PInvoke signatures for Win32's Process API.
+ ///
+ static class ProcessApi
+ {
+ internal const uint EXTENDED_STARTUPINFO_PRESENT = 0x00080000;
+
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
+ internal struct STARTUPINFOEX
+ {
+ public STARTUPINFO StartupInfo;
+ public IntPtr lpAttributeList;
+ }
+
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
+ internal struct STARTUPINFO
+ {
+ public int cb;
+ public string lpReserved;
+ public string lpDesktop;
+ public string lpTitle;
+ public int dwX;
+ public int dwY;
+ public int dwXSize;
+ public int dwYSize;
+ public int dwXCountChars;
+ public int dwYCountChars;
+ public int dwFillAttribute;
+ public int dwFlags;
+ public short wShowWindow;
+ public short cbReserved2;
+ public IntPtr lpReserved2;
+ public IntPtr hStdInput;
+ public IntPtr hStdOutput;
+ public IntPtr hStdError;
+ }
+
+ [StructLayout(LayoutKind.Sequential)]
+ internal struct PROCESS_INFORMATION
+ {
+ public IntPtr hProcess;
+ public IntPtr hThread;
+ public int dwProcessId;
+ public int dwThreadId;
+ }
+
+ [StructLayout(LayoutKind.Sequential)]
+ internal struct SECURITY_ATTRIBUTES
+ {
+ public int nLength;
+ public IntPtr lpSecurityDescriptor;
+ public int bInheritHandle;
+ }
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ internal static extern bool InitializeProcThreadAttributeList(
+ IntPtr lpAttributeList, int dwAttributeCount, int dwFlags, ref IntPtr lpSize);
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ internal static extern bool UpdateProcThreadAttribute(
+ IntPtr lpAttributeList, uint dwFlags, IntPtr attribute, IntPtr lpValue,
+ IntPtr cbSize, IntPtr lpPreviousValue, IntPtr lpReturnSize);
+
+ [DllImport("kernel32.dll")]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ internal static extern bool CreateProcess(
+ string lpApplicationName, string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
+ ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags,
+ IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFOEX lpStartupInfo,
+ out PROCESS_INFORMATION lpProcessInformation);
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ internal static extern bool DeleteProcThreadAttributeList(IntPtr lpAttributeList);
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ internal static extern bool CloseHandle(IntPtr hObject);
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Native/PseudoConsoleApi.cs b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Native/PseudoConsoleApi.cs
new file mode 100644
index 0000000000..f0c08f5990
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Native/PseudoConsoleApi.cs
@@ -0,0 +1,30 @@
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.Runtime.InteropServices;
+
+namespace GUIConsole.ConPTY.Native
+{
+ ///
+ /// PInvoke signatures for Win32's PseudoConsole API.
+ ///
+ static class PseudoConsoleApi
+ {
+ internal const uint PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE = 0x00020016;
+
+ [StructLayout(LayoutKind.Sequential)]
+ internal struct COORD
+ {
+ public short X;
+ public short Y;
+ }
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ internal static extern int CreatePseudoConsole(COORD size, SafeFileHandle hInput, SafeFileHandle hOutput, uint dwFlags, out IntPtr phPC);
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ internal static extern int ClosePseudoConsole(IntPtr hPC);
+
+ [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
+ internal static extern bool CreatePipe(out SafeFileHandle hReadPipe, out SafeFileHandle hWritePipe, IntPtr lpPipeAttributes, int nSize);
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Processes/Process.cs b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Processes/Process.cs
new file mode 100644
index 0000000000..14bfb03135
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Processes/Process.cs
@@ -0,0 +1,70 @@
+using System;
+using System.Runtime.InteropServices;
+using static GUIConsole.ConPTY.Native.ProcessApi;
+
+namespace GUIConsole.ConPTY.Processes
+{
+ ///
+ /// Represents an instance of a process.
+ ///
+ internal sealed class Process : IDisposable
+ {
+ public Process(STARTUPINFOEX startupInfo, PROCESS_INFORMATION processInfo)
+ {
+ StartupInfo = startupInfo;
+ ProcessInfo = processInfo;
+ }
+
+ public STARTUPINFOEX StartupInfo { get; }
+ public PROCESS_INFORMATION ProcessInfo { get; }
+
+ #region IDisposable Support
+
+ private bool disposedValue = false; // To detect redundant calls
+
+ void Dispose(bool disposing)
+ {
+ if (!disposedValue)
+ {
+ if (disposing)
+ {
+ // dispose managed state (managed objects).
+ }
+
+ // dispose unmanaged state
+
+ // Free the attribute list
+ if (StartupInfo.lpAttributeList != IntPtr.Zero)
+ {
+ DeleteProcThreadAttributeList(StartupInfo.lpAttributeList);
+ Marshal.FreeHGlobal(StartupInfo.lpAttributeList);
+ }
+
+ // Close process and thread handles
+ if (ProcessInfo.hProcess != IntPtr.Zero)
+ {
+ CloseHandle(ProcessInfo.hProcess);
+ }
+ if (ProcessInfo.hThread != IntPtr.Zero)
+ {
+ CloseHandle(ProcessInfo.hThread);
+ }
+
+ disposedValue = true;
+ }
+ }
+
+ ~Process()
+ {
+ Dispose(false);
+ }
+
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ #endregion
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Processes/ProcessFactory.cs b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Processes/ProcessFactory.cs
new file mode 100644
index 0000000000..06e99e9d9c
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Processes/ProcessFactory.cs
@@ -0,0 +1,99 @@
+using System;
+using System.ComponentModel;
+using System.Runtime.InteropServices;
+using static GUIConsole.ConPTY.Native.ProcessApi;
+
+namespace GUIConsole.ConPTY.Processes
+{
+ ///
+ /// Support for starting and configuring processes.
+ ///
+ ///
+ /// Possible to replace with managed code? The key is being able to provide the PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE attribute
+ ///
+ static class ProcessFactory
+ {
+ ///
+ /// Start and configure a process. The return value represents the process and should be disposed.
+ ///
+ internal static Process Start(string command, IntPtr attributes, IntPtr hPC)
+ {
+ var startupInfo = ConfigureProcessThread(hPC, attributes);
+ var processInfo = RunProcess(ref startupInfo, command);
+ return new Process(startupInfo, processInfo);
+ }
+
+ private static STARTUPINFOEX ConfigureProcessThread(IntPtr hPC, IntPtr attributes)
+ {
+ // this method implements the behavior described in https://docs.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session#preparing-for-creation-of-the-child-process
+
+ var lpSize = IntPtr.Zero;
+ var success = InitializeProcThreadAttributeList(
+ lpAttributeList: IntPtr.Zero,
+ dwAttributeCount: 1,
+ dwFlags: 0,
+ lpSize: ref lpSize
+ );
+ if (success || lpSize == IntPtr.Zero) // we're not expecting `success` here, we just want to get the calculated lpSize
+ {
+ throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not calculate the number of bytes for the attribute list.");
+ }
+
+ var startupInfo = new STARTUPINFOEX();
+ startupInfo.StartupInfo.cb = Marshal.SizeOf();
+ startupInfo.lpAttributeList = Marshal.AllocHGlobal(lpSize);
+
+ success = InitializeProcThreadAttributeList(
+ lpAttributeList: startupInfo.lpAttributeList,
+ dwAttributeCount: 1,
+ dwFlags: 0,
+ lpSize: ref lpSize
+ );
+ if (!success)
+ {
+ throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not set up attribute list.");
+ }
+
+ success = UpdateProcThreadAttribute(
+ lpAttributeList: startupInfo.lpAttributeList,
+ dwFlags: 0,
+ attribute: attributes,
+ lpValue: hPC,
+ cbSize: (IntPtr)IntPtr.Size,
+ lpPreviousValue: IntPtr.Zero,
+ lpReturnSize: IntPtr.Zero
+ );
+ if (!success)
+ {
+ throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not set pseudoconsole thread attribute.");
+ }
+
+ return startupInfo;
+ }
+
+ private static PROCESS_INFORMATION RunProcess(ref STARTUPINFOEX sInfoEx, string commandLine)
+ {
+ int securityAttributeSize = Marshal.SizeOf();
+ var pSec = new SECURITY_ATTRIBUTES { nLength = securityAttributeSize };
+ var tSec = new SECURITY_ATTRIBUTES { nLength = securityAttributeSize };
+ var success = CreateProcess(
+ lpApplicationName: null,
+ lpCommandLine: commandLine,
+ lpProcessAttributes: ref pSec,
+ lpThreadAttributes: ref tSec,
+ bInheritHandles: false,
+ dwCreationFlags: EXTENDED_STARTUPINFO_PRESENT,
+ lpEnvironment: IntPtr.Zero,
+ lpCurrentDirectory: null,
+ lpStartupInfo: ref sInfoEx,
+ lpProcessInformation: out PROCESS_INFORMATION pInfo
+ );
+ if (!success)
+ {
+ throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not create process.");
+ }
+
+ return pInfo;
+ }
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/PseudoConsole.cs b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/PseudoConsole.cs
new file mode 100644
index 0000000000..eaabb07f02
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/PseudoConsole.cs
@@ -0,0 +1,40 @@
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.ComponentModel;
+using static GUIConsole.ConPTY.Native.PseudoConsoleApi;
+
+namespace GUIConsole.ConPTY
+{
+ ///
+ /// Utility functions around the new Pseudo Console APIs.
+ ///
+ internal sealed class PseudoConsole : IDisposable
+ {
+ public static readonly IntPtr PseudoConsoleThreadAttribute = (IntPtr)PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE;
+
+ public IntPtr Handle { get; }
+
+ private PseudoConsole(IntPtr handle)
+ {
+ this.Handle = handle;
+ }
+
+ internal static PseudoConsole Create(SafeFileHandle inputReadSide, SafeFileHandle outputWriteSide, int width, int height)
+ {
+ var createResult = CreatePseudoConsole(
+ new COORD { X = (short)width, Y = (short)height },
+ inputReadSide, outputWriteSide,
+ 0, out IntPtr hPC);
+ if(createResult != 0)
+ {
+ throw new Win32Exception(createResult, "Could not create pseudo console.");
+ }
+ return new PseudoConsole(hPC);
+ }
+
+ public void Dispose()
+ {
+ ClosePseudoConsole(Handle);
+ }
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/PseudoConsolePipe.cs b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/PseudoConsolePipe.cs
new file mode 100644
index 0000000000..5716e24c48
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/PseudoConsolePipe.cs
@@ -0,0 +1,48 @@
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.ComponentModel;
+using System.Runtime.InteropServices;
+using static GUIConsole.ConPTY.Native.PseudoConsoleApi;
+
+namespace GUIConsole.ConPTY
+{
+ ///
+ /// A pipe used to talk to the pseudoconsole, as described in:
+ /// https://docs.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session
+ ///
+ ///
+ /// We'll have two instances of this class, one for input and one for output.
+ ///
+ internal sealed class PseudoConsolePipe : IDisposable
+ {
+ public readonly SafeFileHandle ReadSide;
+ public readonly SafeFileHandle WriteSide;
+
+ public PseudoConsolePipe()
+ {
+ if (!CreatePipe(out ReadSide, out WriteSide, IntPtr.Zero, 0))
+ {
+ throw new Win32Exception(Marshal.GetLastWin32Error(), "failed to create pipe");
+ }
+ }
+
+ #region IDisposable
+
+ void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ ReadSide?.Dispose();
+ WriteSide?.Dispose();
+ }
+ }
+
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ #endregion
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Terminal.cs b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Terminal.cs
new file mode 100644
index 0000000000..bedc7b117b
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.ConPTY/Terminal.cs
@@ -0,0 +1,113 @@
+using GUIConsole.ConPTY.Processes;
+using Microsoft.Win32.SafeHandles;
+using System;
+using System.IO;
+using System.Threading;
+using static GUIConsole.ConPTY.Native.ConsoleApi;
+
+namespace GUIConsole.ConPTY
+{
+ ///
+ /// Class for managing communication with the underlying console, and communicating with its pseudoconsole.
+ ///
+ public sealed class Terminal
+ {
+ private const string ExitCommand = "exit\r";
+ private const string CtrlC_Command = "\x3";
+ private SafeFileHandle _consoleInputPipeWriteHandle;
+ private StreamWriter _consoleInputWriter;
+
+ ///
+ /// A stream of VT-100-enabled output from the console.
+ ///
+ public FileStream ConsoleOutStream { get; private set; }
+
+ ///
+ /// Fired once the console has been hooked up and is ready to receive input.
+ ///
+ public event EventHandler OutputReady;
+
+ public Terminal()
+ {
+
+ }
+
+ ///
+ /// Start the psuedoconsole and run the process as shown in
+ /// https://docs.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session#creating-the-pseudoconsole
+ ///
+ /// the command to run, e.g. cmd.exe
+ /// The height (in characters) to start the pseudoconsole with. Defaults to 80.
+ /// The width (in characters) to start the pseudoconsole with. Defaults to 30.
+ public void Start(string command, int consoleWidth = 80, int consoleHeight = 30)
+ {
+ using (var inputPipe = new PseudoConsolePipe())
+ using (var outputPipe = new PseudoConsolePipe())
+ using (var pseudoConsole = PseudoConsole.Create(inputPipe.ReadSide, outputPipe.WriteSide, consoleWidth, consoleHeight))
+ using (var process = ProcessFactory.Start(command, PseudoConsole.PseudoConsoleThreadAttribute, pseudoConsole.Handle))
+ {
+ // copy all pseudoconsole output to a FileStream and expose it to the rest of the app
+ ConsoleOutStream = new FileStream(outputPipe.ReadSide, FileAccess.Read);
+ OutputReady.Invoke(this, EventArgs.Empty);
+
+ // Store input pipe handle, and a writer for later reuse
+ _consoleInputPipeWriteHandle = inputPipe.WriteSide;
+ _consoleInputWriter = new StreamWriter(new FileStream(_consoleInputPipeWriteHandle, FileAccess.Write))
+ {
+ AutoFlush = true
+ };
+
+ // free resources in case the console is ungracefully closed (e.g. by the 'x' in the window titlebar)
+ OnClose(() => DisposeResources(process, pseudoConsole, outputPipe, inputPipe, _consoleInputWriter));
+
+ WaitForExit(process).WaitOne(Timeout.Infinite);
+ }
+ }
+
+ ///
+ /// Sends the given string to the anonymous pipe that writes to the active pseudoconsole.
+ ///
+ /// A string of characters to write to the console. Supports VT-100 codes.
+ public void WriteToPseudoConsole(string input)
+ {
+ if (_consoleInputWriter == null)
+ {
+ throw new InvalidOperationException("There is no writer attached to a pseudoconsole. Have you called Start on this instance yet?");
+ }
+ _consoleInputWriter.Write(input);
+ }
+
+ ///
+ /// Get an AutoResetEvent that signals when the process exits
+ ///
+ private static AutoResetEvent WaitForExit(Process process) =>
+ new AutoResetEvent(false)
+ {
+ SafeWaitHandle = new SafeWaitHandle(process.ProcessInfo.hProcess, ownsHandle: false)
+ };
+
+ ///
+ /// Set a callback for when the terminal is closed (e.g. via the "X" window decoration button).
+ /// Intended for resource cleanup logic.
+ ///
+ private static void OnClose(Action handler)
+ {
+ SetConsoleCtrlHandler(eventType =>
+ {
+ if (eventType == CtrlTypes.CTRL_CLOSE_EVENT)
+ {
+ handler();
+ }
+ return false;
+ }, true);
+ }
+
+ private void DisposeResources(params IDisposable[] disposables)
+ {
+ foreach (var disposable in disposables)
+ {
+ disposable.Dispose();
+ }
+ }
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.WPF/App.config b/samples/ConPTY/GUIConsole/GUIConsole.WPF/App.config
new file mode 100644
index 0000000000..bae5d6d814
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.WPF/App.config
@@ -0,0 +1,6 @@
+
+
+
+
+
+
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.WPF/App.xaml b/samples/ConPTY/GUIConsole/GUIConsole.WPF/App.xaml
new file mode 100644
index 0000000000..e95139446b
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.WPF/App.xaml
@@ -0,0 +1,8 @@
+
+
+
+
+
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.WPF/App.xaml.cs b/samples/ConPTY/GUIConsole/GUIConsole.WPF/App.xaml.cs
new file mode 100644
index 0000000000..be938d6d4e
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.WPF/App.xaml.cs
@@ -0,0 +1,15 @@
+using System.Windows;
+
+namespace GUIConsole.Wpf
+{
+ ///
+ /// Interaction logic for App.xaml
+ ///
+ public partial class App : Application
+ {
+ public App()
+ {
+
+ }
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.WPF/GUIConsole.WPF.csproj b/samples/ConPTY/GUIConsole/GUIConsole.WPF/GUIConsole.WPF.csproj
new file mode 100644
index 0000000000..f561ba6fb3
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.WPF/GUIConsole.WPF.csproj
@@ -0,0 +1,105 @@
+
+
+
+
+ Debug
+ AnyCPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}
+ WinExe
+ GUIConsole.Wpf
+ GUIConsole.Wpf
+ v4.6.1
+ 512
+ {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
+ 4
+ true
+ true
+
+
+
+ AnyCPU
+ true
+ full
+ false
+ bin\Debug\
+ DEBUG;TRACE
+ prompt
+ 4
+
+
+ AnyCPU
+ pdbonly
+ true
+ bin\Release\
+ TRACE
+ prompt
+ 4
+
+
+
+
+
+
+
+
+
+
+
+ 4.0
+
+
+
+
+
+
+
+ MSBuild:Compile
+ Designer
+
+
+ MSBuild:Compile
+ Designer
+
+
+ App.xaml
+ Code
+
+
+ MainWindow.xaml
+ Code
+
+
+
+
+ Code
+
+
+ True
+ True
+ Resources.resx
+
+
+ True
+ Settings.settings
+ True
+
+
+ ResXFileCodeGenerator
+ Resources.Designer.cs
+
+
+ SettingsSingleFileGenerator
+ Settings.Designer.cs
+
+
+
+
+
+
+
+ {96634c74-0c52-4381-9477-97e1d58fe5b5}
+ GUIConsole.ConPTY
+
+
+
+
\ No newline at end of file
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.WPF/MainWindow.xaml b/samples/ConPTY/GUIConsole/GUIConsole.WPF/MainWindow.xaml
new file mode 100644
index 0000000000..766abcfca8
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.WPF/MainWindow.xaml
@@ -0,0 +1,84 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ GUIConsole
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.WPF/MainWindow.xaml.cs b/samples/ConPTY/GUIConsole/GUIConsole.WPF/MainWindow.xaml.cs
new file mode 100644
index 0000000000..2cfdf88f56
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.WPF/MainWindow.xaml.cs
@@ -0,0 +1,121 @@
+using GUIConsole.ConPTY;
+using System;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+
+namespace GUIConsole.Wpf
+{
+ public partial class MainWindow : Window
+ {
+ private Terminal _terminal;
+
+ public MainWindow()
+ {
+ InitializeComponent();
+ }
+
+ private void Window_Loaded(object sender, RoutedEventArgs e)
+ {
+ // Start up the console, and point it to cmd.exe.
+ _terminal = new Terminal();
+ Task.Run(() => _terminal.Start("powershell.exe"));
+ _terminal.OutputReady += Terminal_OutputReady;
+ }
+
+ private void Terminal_OutputReady(object sender, EventArgs e)
+ {
+ // Start a long-lived thread for the "read console" task, so that we don't use a standard thread pool thread.
+ Task.Factory.StartNew(() => CopyConsoleToWindow(), TaskCreationOptions.LongRunning);
+
+ Dispatcher.Invoke(() => { TitleBarTitle.Text = "GUIConsole - powershell.exe"; });
+ }
+
+ private void CopyConsoleToWindow()
+ {
+ using (StreamReader reader = new StreamReader(_terminal.ConsoleOutStream))
+ {
+ // Read the console's output 1 character at a time
+ int bytesRead;
+ char[] buf = new char[1];
+ while ((bytesRead = reader.ReadBlock(buf, 0, 1)) != 0)
+ {
+ // This is where you'd parse and tokenize the incoming VT100 text, most likely.
+ Dispatcher.Invoke(() =>
+ {
+ // ...and then you'd do something to render it.
+ // For now, just emit raw VT100 to the primary TextBlock.
+ TerminalHistoryBlock.Text += new string(buf.Take(bytesRead).ToArray());
+ });
+ }
+ }
+ }
+
+ private void Window_KeyDown(object sender, KeyEventArgs e)
+ {
+ if (!e.Handled)
+ {
+ // This is where you'd take the pressed key, and convert it to a
+ // VT100 code before sending it along. For now, we'll just send _something_.
+ _terminal.WriteToPseudoConsole(e.Key.ToString());
+ }
+ }
+
+ private bool _autoScroll = true;
+ private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
+ {
+ // User scrolled...
+ if (e.ExtentHeightChange == 0)
+ {
+ //...down to the bottom. Re-engage autoscrolling.
+ if (TerminalHistoryViewer.VerticalOffset == TerminalHistoryViewer.ScrollableHeight)
+ {
+ _autoScroll = true;
+ }
+ //...elsewhere. Disengage autoscrolling.
+ else
+ {
+ _autoScroll = false;
+ }
+
+ // Autoscrolling is enabled, and content caused scrolling:
+ if (_autoScroll && e.ExtentHeightChange != 0)
+ {
+ TerminalHistoryViewer.ScrollToEnd();
+ }
+ }
+ }
+
+ private void Window_MouseDown(object sender, MouseButtonEventArgs e)
+ {
+ if (e.ChangedButton == MouseButton.Left) { DragMove(); }
+ }
+
+ private void MaximizeRestoreButton_Click(object sender, RoutedEventArgs e)
+ {
+ if (WindowState == WindowState.Normal)
+ {
+ WindowState = WindowState.Maximized;
+ MaximizeRestoreButton.Content = "\uE923";
+ }
+ else if (WindowState == WindowState.Maximized)
+ {
+ WindowState = WindowState.Normal;
+ MaximizeRestoreButton.Content = "\uE922";
+ }
+ }
+
+ private void MinimizeButton_Click(object sender, RoutedEventArgs e)
+ {
+ WindowState = WindowState.Minimized;
+ }
+
+ private void CloseButton_Click(object sender, RoutedEventArgs e)
+ {
+ this.Close();
+ }
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/AssemblyInfo.cs b/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000000..f750f0af36
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/AssemblyInfo.cs
@@ -0,0 +1,55 @@
+using System.Reflection;
+using System.Resources;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Windows;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("GUIConsole.Wpf")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("GUIConsole.Wpf")]
+[assembly: AssemblyCopyright("Copyright © 2018")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+//In order to begin building localizable applications, set
+//CultureYouAreCodingWith in your .csproj file
+//inside a . For example, if you are using US english
+//in your source files, set the to en-US. Then uncomment
+//the NeutralResourceLanguage attribute below. Update the "en-US" in
+//the line below to match the UICulture setting in the project file.
+
+//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
+
+
+[assembly: ThemeInfo(
+ ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
+ //(used if a resource is not found in the page,
+ // or application resource dictionaries)
+ ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
+ //(used if a resource is not found in the page,
+ // app, or any theme specific resource dictionaries)
+)]
+
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Resources.Designer.cs b/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Resources.Designer.cs
new file mode 100644
index 0000000000..b5f7cb0d66
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Resources.Designer.cs
@@ -0,0 +1,63 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by a tool.
+// Runtime Version:4.0.30319.42000
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+namespace GUIConsole.Wpf.Properties {
+ using System;
+
+
+ ///
+ /// A strongly-typed resource class, for looking up localized strings, etc.
+ ///
+ // This class was auto-generated by the StronglyTypedResourceBuilder
+ // class via a tool like ResGen or Visual Studio.
+ // To add or remove a member, edit your .ResX file then rerun ResGen
+ // with the /str option, or rebuild your VS project.
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+ internal class Resources {
+
+ private static global::System.Resources.ResourceManager resourceMan;
+
+ private static global::System.Globalization.CultureInfo resourceCulture;
+
+ [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
+ internal Resources() {
+ }
+
+ ///
+ /// Returns the cached ResourceManager instance used by this class.
+ ///
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Resources.ResourceManager ResourceManager {
+ get {
+ if (object.ReferenceEquals(resourceMan, null)) {
+ global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("GUIConsole.Wpf.Properties.Resources", typeof(Resources).Assembly);
+ resourceMan = temp;
+ }
+ return resourceMan;
+ }
+ }
+
+ ///
+ /// Overrides the current thread's CurrentUICulture property for all
+ /// resource lookups using this strongly typed resource class.
+ ///
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Globalization.CultureInfo Culture {
+ get {
+ return resourceCulture;
+ }
+ set {
+ resourceCulture = value;
+ }
+ }
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Resources.resx b/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Resources.resx
new file mode 100644
index 0000000000..71f71657bc
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Resources.resx
@@ -0,0 +1,117 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Settings.Designer.cs b/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Settings.Designer.cs
new file mode 100644
index 0000000000..df722444aa
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Settings.Designer.cs
@@ -0,0 +1,26 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by a tool.
+// Runtime Version:4.0.30319.42000
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+namespace GUIConsole.Wpf.Properties {
+
+
+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.0.0.0")]
+ internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
+
+ private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
+
+ public static Settings Default {
+ get {
+ return defaultInstance;
+ }
+ }
+ }
+}
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Settings.settings b/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Settings.settings
new file mode 100644
index 0000000000..c2dbd5cad3
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.WPF/Properties/Settings.settings
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
diff --git a/samples/ConPTY/GUIConsole/GUIConsole.sln b/samples/ConPTY/GUIConsole/GUIConsole.sln
new file mode 100644
index 0000000000..8b11107e4e
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/GUIConsole.sln
@@ -0,0 +1,61 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 15
+VisualStudioVersion = 15.0.26124.0
+MinimumVisualStudioVersion = 15.0.26124.0
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GUIConsole.WPF", "GUIConsole.WPF\GUIConsole.WPF.csproj", "{FD2109FE-F78A-4E31-8317-11D1B66B69AF}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GUIConsole.ConPTY", "GUIConsole.ConPTY\GUIConsole.ConPTY.csproj", "{96634C74-0C52-4381-9477-97E1D58FE5B5}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Debug|ARM = Debug|ARM
+ Debug|x64 = Debug|x64
+ Debug|x86 = Debug|x86
+ Release|Any CPU = Release|Any CPU
+ Release|ARM = Release|ARM
+ Release|x64 = Release|x64
+ Release|x86 = Release|x86
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Debug|ARM.ActiveCfg = Debug|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Debug|ARM.Build.0 = Debug|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Debug|x64.ActiveCfg = Debug|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Debug|x64.Build.0 = Debug|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Debug|x86.ActiveCfg = Debug|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Debug|x86.Build.0 = Debug|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Release|Any CPU.Build.0 = Release|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Release|ARM.ActiveCfg = Release|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Release|ARM.Build.0 = Release|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Release|x64.ActiveCfg = Release|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Release|x64.Build.0 = Release|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Release|x86.ActiveCfg = Release|Any CPU
+ {FD2109FE-F78A-4E31-8317-11D1B66B69AF}.Release|x86.Build.0 = Release|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Debug|ARM.ActiveCfg = Debug|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Debug|ARM.Build.0 = Debug|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Debug|x64.ActiveCfg = Debug|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Debug|x64.Build.0 = Debug|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Debug|x86.ActiveCfg = Debug|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Debug|x86.Build.0 = Debug|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Release|Any CPU.Build.0 = Release|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Release|ARM.ActiveCfg = Release|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Release|ARM.Build.0 = Release|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Release|x64.ActiveCfg = Release|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Release|x64.Build.0 = Release|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Release|x86.ActiveCfg = Release|Any CPU
+ {96634C74-0C52-4381-9477-97E1D58FE5B5}.Release|x86.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {0066B3A2-194D-471B-A56D-E25BB5AC0EB4}
+ EndGlobalSection
+EndGlobal
diff --git a/samples/ConPTY/GUIConsole/README.md b/samples/ConPTY/GUIConsole/README.md
new file mode 100644
index 0000000000..97fb35e5a1
--- /dev/null
+++ b/samples/ConPTY/GUIConsole/README.md
@@ -0,0 +1,9 @@
+# GUIConsole
+
+This is an example of what the skeleton of a custom WPF console might look like.
+
+The `GUIConsole.WPF` project is a WPF application targeting .NET 4.6.1. It creates a single WPF `Window` that acts as the console, and keeps the underlying console visible.
+
+The `GUIConsole.ConPTY` project is a .NET Standard 2.0 library that handles the creation of the console, and enables pseudoconsole behavior. `Terminal.cs` contains the publicly visible pieces that the WPF application will interact with. `Terminal.cs` exposes two things that allow reading from, and writing to, the console:
+ * `ConsoleOutStream`, a `FileStream` hooked up to the pseudoconsole's output pipe. This will output VT100.
+ * `WriteToPseudoConsole(string input)`, a method that will take the given string and write it to the pseudoconsole via its input pipe. This accepts VT100.