protected virtual void Dispose(bool disposing) { if (disposed) { return; } // Free the attribute list if (StartupInfo.lpAttributeList != IntPtr.Zero) { ProcessApi.DeleteProcThreadAttributeList(StartupInfo.lpAttributeList); Marshal.FreeHGlobal(StartupInfo.lpAttributeList); } // Close process and thread handles if (ProcessInfo.hProcess != IntPtr.Zero) { ConsoleApi.CloseHandle(ProcessInfo.hProcess); } if (ProcessInfo.hThread != IntPtr.Zero) { ConsoleApi.CloseHandle(ProcessInfo.hThread); } disposed = true; }
public Pipe(SecurityAttributes securityAttributes) { if (!ConsoleApi.CreatePipe(out read, out write, ref securityAttributes, 0)) { throw new InteropException("Failed to create pipe.", Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error())); } }
private static void SetConsoleModeToVirtualTerminal() { SafeFileHandle stdIn = ConsoleApi.GetStdHandle(StdHandle.InputHandle); if (!ConsoleApi.GetConsoleMode(stdIn, out uint outConsoleMode)) { throw InteropException.CreateWithInnerHResultException("Could not get console mode."); } outConsoleMode |= Constants.ENABLE_VIRTUAL_TERMINAL_PROCESSING | Constants.DISABLE_NEWLINE_AUTO_RETURN; if (!ConsoleApi.SetConsoleMode(stdIn, outConsoleMode)) { throw InteropException.CreateWithInnerHResultException("Could not enable virtual terminal processing."); } }
private void MakeHandleNoninheritable(ref SafeFileHandle handler, IntPtr processHandle) { // Create noninheritable read handle and close the inheritable read handle. IntPtr handleClone; if (!ConsoleApi.DuplicateHandle( processHandle, handler.DangerousGetHandle(), processHandle, out handleClone, 0, false, Constants.DUPLICATE_SAME_ACCESS)) { throw InteropException.CreateWithInnerHResultException("Couldn't duplicate the handle."); } SafeFileHandle toRelease = handler; handler = new SafeFileHandle(handleClone, true); toRelease.Dispose(); }