PseudoConsole.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Microsoft.Win32.SafeHandles;
  2. using System;
  3. using static MiniTerm.Native.PseudoConsoleApi;
  4. namespace MiniTerm
  5. {
  6. /// <summary>
  7. /// Utility functions around the new Pseudo Console APIs
  8. /// </summary>
  9. internal sealed class PseudoConsole : IDisposable
  10. {
  11. public static readonly IntPtr PseudoConsoleThreadAttribute = (IntPtr)PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE;
  12. public IntPtr Handle { get; }
  13. private PseudoConsole(IntPtr handle)
  14. {
  15. this.Handle = handle;
  16. }
  17. internal static PseudoConsole Create(SafeFileHandle inputReadSide, SafeFileHandle outputWriteSide, int width, int height)
  18. {
  19. var createResult = CreatePseudoConsole(
  20. new COORD { X = (short)width, Y = (short)height },
  21. inputReadSide, outputWriteSide,
  22. 0, out IntPtr hPC);
  23. if(createResult != 0)
  24. {
  25. throw new InvalidOperationException("Could not create psuedo console. Error Code " + createResult);
  26. }
  27. return new PseudoConsole(hPC);
  28. }
  29. public void Dispose()
  30. {
  31. ClosePseudoConsole(Handle);
  32. }
  33. }
  34. }