Skip to content

Commit

Permalink
Implement ProcessStartInfo.InheritHandles
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinWise committed Nov 19, 2024
1 parent 161f346 commit d94dbac
Show file tree
Hide file tree
Showing 14 changed files with 464 additions and 151 deletions.
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 @@ -306,6 +306,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

0 comments on commit d94dbac

Please sign in to comment.