terminal/samples/ConPTY/MiniTerm/MiniTerm/PseudoConsole.cs
Will Fuqua 637c57473e
add c# files
- Move from rather ad-hoc, error-prone resource management to IDisposable, which should give us a bit more enforcement.
- Optimistically remove "buggy" from readme because the known bugs are now fixed! The main source of bugs was the incorrect InitializeProcThreadAttributeList usage.
- Handle ctrl-c by forwarding it to the PseudoConsole
- Handle terminal close when the window close button is used
- Use .NET's CopyTo in the CopyPipeToOutput, it's much simpler code and seems more robust than the ReadFile/WriteFile approach
- Minor refactor to split native APIs to multiple files
2018-09-20 22:13:37 +07:00

40 lines
1.2 KiB
C#

using Microsoft.Win32.SafeHandles;
using System;
using static MiniTerm.Native.PseudoConsoleApi;
namespace MiniTerm
{
/// <summary>
/// Utility functions around the new Pseudo Console APIs
/// </summary>
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 InvalidOperationException("Could not create psuedo console. Error Code " + createResult);
}
return new PseudoConsole(hPC);
}
public void Dispose()
{
ClosePseudoConsole(Handle);
}
}
}