// This method is only exposed via methods to get at the console. // We won't use any security checks here. private static Stream GetStandardFile(int stdHandleName, FileAccess access, int bufferSize) { IntPtr handle = Win32Native.GetStdHandle(stdHandleName); // If someone launches a managed process via CreateProcess, stdout // stderr, & stdin could independently be set to INVALID_HANDLE_VALUE. if (handle == Win32Native.INVALID_HANDLE_VALUE) { //BCLDebug.ConsoleError("Console::GetStandardFile for handle "+stdHandleName+" failed, with HRESULT: "+Marshal.GetLastWin32Error()+" Setting it to null."); return Stream.Null; } // Zero appears to not be a valid handle. I haven't gotten GetStdHandle // to return INVALID_HANDLE_VALUE, as the docs say. if (handle == IntPtr.Zero) { //BCLDebug.ConsoleError("Console::GetStandardFile for std handle "+stdHandleName+" succeeded but returned 0. Setting it to null."); return Stream.Null; } // Check whether we can read or write to this handle. if (stdHandleName != Win32Native.STD_INPUT_HANDLE && 0==ConsoleHandleIsValidNative(handle)) { //BCLDebug.ConsoleError("Console::ConsoleHandleIsValid for std handle "+stdHandleName+" failed, setting it to a null stream"); return Stream.Null; } //BCLDebug.ConsoleError("Console::GetStandardFile for std handle "+stdHandleName+" succeeded, returning handle number "+handle.ToString()); Stream console = new __ConsoleStream(handle, access); // Do not buffer console streams, or we can get into situations where // we end up blocking waiting for you to hit enter twice. It was // a bad idea & generally redundant. return console; }
private static Stream GetStandardFile(int stdHandleName, FileAccess access, int bufferSize) { // We shouldn't close the handle for stdout, etc, or we'll break // unmanaged code in the process that will print to console. // We should have a better way of marking this on SafeHandle. IntPtr handle = Win32Native.GetStdHandle(stdHandleName); SafeFileHandle sh = new SafeFileHandle(handle, false); // If someone launches a managed process via CreateProcess, stdout // stderr, & stdin could independently be set to INVALID_HANDLE_VALUE. // Additionally they might use 0 as an invalid handle. if (sh.IsInvalid) { // Minor perf optimization - get it out of the finalizer queue. sh.SetHandleAsInvalid(); return Stream.Null; } // Check whether we can read or write to this handle. if (stdHandleName != Win32Native.STD_INPUT_HANDLE && !ConsoleHandleIsValid(sh)) { //BCLDebug.ConsoleError("Console::ConsoleHandleIsValid for std handle "+stdHandleName+" failed, setting it to a null stream"); return Stream.Null; } //BCLDebug.ConsoleError("Console::GetStandardFile for std handle "+stdHandleName+" succeeded, returning handle number "+handle.ToString()); Stream console = new __ConsoleStream(sh, access); // Do not buffer console streams, or we can get into situations where // we end up blocking waiting for you to hit enter twice. It was // redundant. return console; }
private static Stream GetStandardFile(int stdHandleName, FileAccess access, int bufferSize) { Stream console = new __ConsoleStream(null, access); return console; }