Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ProcessStartInfo.InheritHandles for Windows #107836

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ internal static partial class StartupInfoOptions
internal const int STARTF_USESHOWWINDOW = 0x00000001;
internal const int STARTF_USESTDHANDLES = 0x00000100;
internal const int CREATE_UNICODE_ENVIRONMENT = 0x00000400;
internal const int EXTENDED_STARTUPINFO_PRESENT = 0x00080000;
internal const int CREATE_NO_WINDOW = 0x08000000;
}

internal static partial class ProcThreadAttribute
{
internal const int HANDLE_LIST = 131074;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,21 @@ internal static unsafe partial bool CreateProcess(
int dwCreationFlags,
IntPtr lpEnvironment,
string? lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
ref STARTUPINFOEX lpStartupInfo,
ref PROCESS_INFORMATION lpProcessInformation
);

[LibraryImport(Libraries.Kernel32, EntryPoint = "InitializeProcThreadAttributeList", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static unsafe partial bool InitializeProcThreadAttributeList(byte* lpAttributeList, uint dwAttributeCount, uint dwFlags, nuint* lpSize);

[LibraryImport(Libraries.Kernel32, EntryPoint = "UpdateProcThreadAttribute", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static unsafe partial bool UpdateProcThreadAttribute(byte* lpAttributeList, uint dwFlags, nuint Attribute, Span<nint> lpValue, nuint cbSize, void* lpPreviousValue, nuint* lpReturnSize);

[LibraryImport(Libraries.Kernel32, EntryPoint = "DeleteProcThreadAttributeList", SetLastError = true)]
internal static unsafe partial void DeleteProcThreadAttributeList(byte* lpAttributeList);

[StructLayout(LayoutKind.Sequential)]
internal struct PROCESS_INFORMATION
{
Expand Down Expand Up @@ -56,5 +67,12 @@ internal struct STARTUPINFO
internal IntPtr hStdOutput;
internal IntPtr hStdError;
}

[StructLayout(LayoutKind.Sequential)]
internal unsafe struct STARTUPINFOEX
{
internal STARTUPINFO StartupInfo;
internal byte* AttributeList;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public ProcessStartInfo(string fileName, System.Collections.Generic.IEnumerable<
[System.ComponentModel.EditorAttribute("System.Diagnostics.Design.StartFileNameEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.Diagnostics.CodeAnalysis.AllowNullAttribute]
public string FileName { get { throw null; } set { } }
public bool InheritHandles { get { throw null; } [System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")] set { } }
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
public bool LoadUserProfile { get { throw null; } set { } }
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@
<data name="CantUseEnvVars" xml:space="preserve">
<value>The Process object must have the UseShellExecute property set to false in order to use environment variables.</value>
</data>
<data name="CantDisableHandleInheritanceAndUseShellExecute" xml:space="preserve">
<value>The Process object must have the UseShellExecute property set to false in order to disable handle inheritance.</value>
</data>
<data name="CantDisableHandleInheritanceAndUseUserName" xml:space="preserve">
<value>The Process object must have the InheritHandles property set to true in order to start a process as a user.</value>
</data>
<data name="UseShellExecuteNotSupported" xml:space="preserve">
<value>UseShellExecute is not supported on this platform.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ private unsafe bool StartWithShellExecuteEx(ProcessStartInfo startInfo)
if (startInfo._environmentVariables != null)
throw new InvalidOperationException(SR.CantUseEnvVars);

if (!startInfo.InheritHandles)
throw new InvalidOperationException(SR.CantDisableHandleInheritanceAndUseShellExecute);

string arguments = startInfo.BuildArguments();

fixed (char* fileName = startInfo.FileName.Length > 0 ? startInfo.FileName : null)
Expand Down
Loading
Loading