@@ -2421,45 +2421,40 @@ private static byte[] ConvertEnvVarsToByteArray(StringDictionary sd)
24212421
24222422 private void SetStartupInfo ( ProcessStartInfo startinfo , ref ProcessNativeMethods . STARTUPINFO lpStartupInfo , ref int creationFlags )
24232423 {
2424+ bool hasRedirection = false ;
24242425 // RedirectionStandardInput
24252426 if ( _redirectstandardinput != null )
24262427 {
2428+ hasRedirection = true ;
24272429 startinfo . RedirectStandardInput = true ;
24282430 _redirectstandardinput = ResolveFilePath ( _redirectstandardinput ) ;
24292431 lpStartupInfo . hStdInput = GetSafeFileHandleForRedirection ( _redirectstandardinput , FileMode . Open ) ;
24302432 }
2431- else
2432- {
2433- lpStartupInfo . hStdInput = new SafeFileHandle ( ProcessNativeMethods . GetStdHandle ( - 10 ) , false ) ;
2434- }
24352433
24362434 // RedirectionStandardOutput
24372435 if ( _redirectstandardoutput != null )
24382436 {
2437+ hasRedirection = true ;
24392438 startinfo . RedirectStandardOutput = true ;
24402439 _redirectstandardoutput = ResolveFilePath ( _redirectstandardoutput ) ;
24412440 lpStartupInfo . hStdOutput = GetSafeFileHandleForRedirection ( _redirectstandardoutput , FileMode . Create ) ;
24422441 }
2443- else
2444- {
2445- lpStartupInfo . hStdOutput = new SafeFileHandle ( ProcessNativeMethods . GetStdHandle ( - 11 ) , false ) ;
2446- }
24472442
24482443 // RedirectionStandardError
24492444 if ( _redirectstandarderror != null )
24502445 {
2446+ hasRedirection = true ;
24512447 startinfo . RedirectStandardError = true ;
24522448 _redirectstandarderror = ResolveFilePath ( _redirectstandarderror ) ;
24532449 lpStartupInfo . hStdError = GetSafeFileHandleForRedirection ( _redirectstandarderror , FileMode . Create ) ;
24542450 }
2455- else
2451+
2452+ if ( hasRedirection )
24562453 {
2457- lpStartupInfo . hStdError = new SafeFileHandle ( ProcessNativeMethods . GetStdHandle ( - 12 ) , false ) ;
2454+ // Set STARTF_USESTDHANDLES only if there is redirection.
2455+ lpStartupInfo . dwFlags = 0x100 ;
24582456 }
24592457
2460- // STARTF_USESTDHANDLES
2461- lpStartupInfo . dwFlags = 0x100 ;
2462-
24632458 if ( startinfo . CreateNoWindow )
24642459 {
24652460 // No new window: Inherit the parent process's console window
@@ -2805,9 +2800,6 @@ internal struct JOBOBJECT_BASIC_PROCESS_ID_LIST
28052800
28062801 internal static class ProcessNativeMethods
28072802 {
2808- [ DllImport ( PinvokeDllNames . GetStdHandleDllName , SetLastError = true ) ]
2809- public static extern IntPtr GetStdHandle ( int whichHandle ) ;
2810-
28112803 [ DllImport ( PinvokeDllNames . CreateProcessWithLogonWDllName , CharSet = CharSet . Unicode , SetLastError = true , ExactSpelling = true ) ]
28122804 [ return : MarshalAs ( UnmanagedType . Bool ) ]
28132805 internal static extern bool CreateProcessWithLogonW ( string userName ,
0 commit comments