Skip to content

Commit 6a90fd0

Browse files
authored
Leave the input/output/error handles unset when they are not redirected (#20853)
1 parent a1ef78d commit 6a90fd0

2 files changed

Lines changed: 11 additions & 21 deletions

File tree

  • src
    • Microsoft.PowerShell.Commands.Management/commands/management
    • Microsoft.PowerShell.ConsoleHost/WindowsTaskbarJumpList

src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

src/Microsoft.PowerShell.ConsoleHost/WindowsTaskbarJumpList/TaskbarJumpList.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ internal static void CreateRunAsAdministratorJumpList()
3333
{
3434
try
3535
{
36-
TaskbarJumpList.CreateElevatedEntry(ConsoleHostStrings.RunAsAdministrator);
36+
CreateElevatedEntry(ConsoleHostStrings.RunAsAdministrator);
3737
}
38-
catch (Exception exception)
38+
catch (Exception)
3939
{
4040
// Due to COM threading complexity there might still be sporadic failures but they can be
4141
// ignored as creating the JumpList is not critical and persists after its first creation.
42-
Debug.Fail($"Creating 'Run as Administrator' JumpList failed. {exception}");
4342
}
4443
});
4544

@@ -48,7 +47,7 @@ internal static void CreateRunAsAdministratorJumpList()
4847
thread.SetApartmentState(ApartmentState.STA);
4948
thread.Start();
5049
}
51-
catch (System.Threading.ThreadStartException)
50+
catch (ThreadStartException)
5251
{
5352
// STA may not be supported on some platforms
5453
}
@@ -117,7 +116,6 @@ private static void CreateElevatedEntry(string title)
117116
var CLSID_EnumerableObjectCollection = new Guid(@"2d3468c1-36a7-43b6-ac24-d3f02fd9607a");
118117
const uint CLSCTX_INPROC_HANDLER = 2;
119118
const uint CLSCTX_INPROC = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER;
120-
var ComSvrInterface_GUID = new Guid(@"555E2D2B-EE00-47AA-AB2B-39F953F6B339");
121119
hResult = CoCreateInstance(ref CLSID_EnumerableObjectCollection, null, CLSCTX_INPROC, ref IID_IUnknown, out object instance);
122120
if (hResult < 0)
123121
{

0 commit comments

Comments
 (0)