11using Microsoft . Win32 . SafeHandles ;
22using System . IO ;
33using 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
79namespace 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