Skip to content

Commit 63bf435

Browse files
gave92Marco Gavelli
authored andcommitted
Switch native methods to CsWin32
1 parent dbb2092 commit 63bf435

File tree

5 files changed

+25
-99
lines changed

5 files changed

+25
-99
lines changed

src/Files.App/Utils/Terminal/ConPTY/Native/ConsoleApi.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/Files.App/Utils/Terminal/ConPTY/Native/PseudoConsoleApi.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Files.App/Utils/Terminal/ConPTY/PseudoConsole.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Win32.SafeHandles;
2-
using System;
3-
using static Files.App.Utils.Terminal.ConPTY.PseudoConsoleApi;
2+
using System.Runtime.InteropServices;
3+
using Windows.Win32.System.Console;
4+
using static Windows.Win32.PInvoke;
45

56
namespace Files.App.Utils.Terminal.ConPTY
67
{
@@ -11,9 +12,9 @@ internal sealed class PseudoConsole : IDisposable
1112
{
1213
public static readonly nint PseudoConsoleThreadAttribute = (nint)PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE;
1314

14-
public nint Handle { get; }
15+
public SafeHandle Handle { get; }
1516

16-
private PseudoConsole(nint handle)
17+
private PseudoConsole(SafeHandle handle)
1718
{
1819
Handle = handle;
1920
}
@@ -23,7 +24,7 @@ internal static PseudoConsole Create(SafeFileHandle inputReadSide, SafeFileHandl
2324
var createResult = CreatePseudoConsole(
2425
new COORD { X = (short)width, Y = (short)height },
2526
inputReadSide, outputWriteSide,
26-
0, out nint hPC);
27+
0, out var hPC);
2728
if (createResult != 0)
2829
{
2930
throw new InvalidOperationException("Could not create pseudo console. Error Code " + createResult);
@@ -38,7 +39,7 @@ internal void Resize(int width, int height)
3839

3940
public void Dispose()
4041
{
41-
ClosePseudoConsole(Handle);
42+
ClosePseudoConsole((HPCON)Handle.DangerousGetHandle());
4243
}
4344
}
4445
}

src/Files.App/Utils/Terminal/ConPTY/PseudoConsolePipe.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Microsoft.Win32.SafeHandles;
22
using System;
3-
using static Files.App.Utils.Terminal.ConPTY.PseudoConsoleApi;
3+
using static Windows.Win32.PInvoke;
44

55
namespace Files.App.Utils.Terminal.ConPTY
66
{
@@ -18,7 +18,7 @@ internal sealed class PseudoConsolePipe : IDisposable
1818

1919
public PseudoConsolePipe()
2020
{
21-
if (!CreatePipe(out ReadSide, out WriteSide, nint.Zero, 0))
21+
if (!CreatePipe(out ReadSide, out WriteSide, null, 0))
2222
{
2323
throw new InvalidOperationException("failed to create pipe");
2424
}

src/Files.App/Utils/Terminal/ConPTY/Terminal.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using Microsoft.Win32.SafeHandles;
22
using System.IO;
33
using System.Runtime.InteropServices;
4-
using Vanara.PInvoke;
5-
using static Files.App.Utils.Terminal.ConPTY.ConsoleApi;
4+
using Windows.Win32.UI.WindowsAndMessaging;
5+
using Windows.Win32.System.Console;
6+
using Windows.Win32.Storage.FileSystem;
7+
using static Windows.Win32.PInvoke;
68

79
namespace Files.App.Utils.Terminal.ConPTY
810
{
@@ -45,7 +47,7 @@ public Terminal()
4547
}
4648

4749
var windowHandle = GetConsoleWindow();
48-
User32.ShowWindow(windowHandle, ShowWindowCommand.SW_HIDE);
50+
ShowWindow(windowHandle, SHOW_WINDOW_CMD.SW_HIDE);
4951

5052
EnableVirtualTerminalSequenceProcessing();
5153
}
@@ -62,12 +64,12 @@ private static void EnableVirtualTerminalSequenceProcessing()
6264
{
6365
//var hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
6466
SafeFileHandle hStdOut = GetConsoleScreenBuffer();
65-
if (!GetConsoleMode(hStdOut, out uint outConsoleMode))
67+
if (!GetConsoleMode(hStdOut, out var outConsoleMode))
6668
{
6769
throw new InvalidOperationException("Could not get console mode");
6870
}
6971

70-
outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
72+
outConsoleMode |= CONSOLE_MODE.ENABLE_VIRTUAL_TERMINAL_PROCESSING | CONSOLE_MODE.DISABLE_NEWLINE_AUTO_RETURN;
7173
if (!SetConsoleMode(hStdOut, outConsoleMode))
7274
{
7375
throw new InvalidOperationException("Could not enable virtual terminal processing");
@@ -84,7 +86,7 @@ public void Start(string command, string directory, int consoleWidth = 80, int c
8486
_inputPipe = new PseudoConsolePipe();
8587
_outputPipe = new PseudoConsolePipe();
8688
_pseudoConsole = PseudoConsole.Create(_inputPipe.ReadSide, _outputPipe.WriteSide, consoleWidth, consoleHeight);
87-
_process = ProcessFactory.Start(command, directory, PseudoConsole.PseudoConsoleThreadAttribute, _pseudoConsole.Handle);
89+
_process = ProcessFactory.Start(command, directory, PseudoConsole.PseudoConsoleThreadAttribute, _pseudoConsole.Handle.DangerousGetHandle());
8890

8991
// copy all pseudoconsole output to a FileStream and expose it to the rest of the app
9092
ConsoleOutStream = new FileStream(_outputPipe.ReadSide, FileAccess.Read);
@@ -134,21 +136,21 @@ private static AutoResetEvent WaitForExit(Process process) =>
134136
/// <remarks>This is described in more detail here: https://docs.microsoft.com/en-us/windows/console/console-handles </remarks>
135137
private static SafeFileHandle GetConsoleScreenBuffer()
136138
{
137-
var file = Kernel32.CreateFile(
139+
var file = CreateFile(
138140
"CONOUT$",
139-
Kernel32.FileAccess.GENERIC_WRITE | Kernel32.FileAccess.GENERIC_READ,
140-
FileShare.Write,
141+
(FILE_ACCESS_FLAGS)(Win32PInvoke.GENERIC_WRITE | Win32PInvoke.GENERIC_READ),
142+
FILE_SHARE_MODE.FILE_SHARE_WRITE,
141143
null,
142-
FileMode.Open,
143-
FileFlagsAndAttributes.FILE_ATTRIBUTE_NORMAL,
144-
nint.Zero);
144+
FILE_CREATION_DISPOSITION.OPEN_EXISTING,
145+
FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL,
146+
new SafeFileHandle());
145147

146-
if (file == new nint(-1))
148+
if (file.IsInvalid)
147149
{
148150
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not get console screen buffer.");
149151
}
150152

151-
return new SafeFileHandle(file.ReleaseOwnership(), true);
153+
return file;
152154
}
153155

154156
#region IDisposable Support

0 commit comments

Comments
 (0)