Skip to content

Commit

Permalink
Simplify interop on Windows CreateProcess path (dotnet/corefx#28372)
Browse files Browse the repository at this point in the history
This makes it a bit smaller, faster and allocate less. And also make it compile with CoreRT.

Commit migrated from dotnet/corefx@909b355
  • Loading branch information
jkotas authored Mar 22, 2018
1 parent 3f6461e commit 328fb66
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ internal static extern bool CreateProcessWithLogonW(
int creationFlags,
IntPtr environmentBlock,
string lpCurrentDirectory,
Interop.Kernel32.STARTUPINFO lpStartupInfo,
[Out] Interop.Kernel32.PROCESS_INFORMATION lpProcessInformation);
ref Interop.Kernel32.STARTUPINFO lpStartupInfo,
ref Interop.Kernel32.PROCESS_INFORMATION lpProcessInformation);

[Flags]
internal enum LogonFlags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,68 +21,40 @@ internal static extern bool CreateProcess(
int dwCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDirectory,
STARTUPINFO lpStartupInfo,
[Out] PROCESS_INFORMATION lpProcessInformation
ref STARTUPINFO lpStartupInfo,
ref PROCESS_INFORMATION lpProcessInformation
);

[StructLayout(LayoutKind.Sequential)]
internal sealed class PROCESS_INFORMATION
internal struct PROCESS_INFORMATION
{
internal IntPtr hProcess = IntPtr.Zero;
internal IntPtr hThread = IntPtr.Zero;
internal int dwProcessId = 0;
internal int dwThreadId = 0;
internal IntPtr hProcess;
internal IntPtr hThread;
internal int dwProcessId;
internal int dwThreadId;
}

[StructLayout(LayoutKind.Sequential)]
internal sealed class STARTUPINFO : IDisposable
internal struct STARTUPINFO
{
internal int cb;
internal IntPtr lpReserved = IntPtr.Zero;
internal IntPtr lpDesktop = IntPtr.Zero;
internal IntPtr lpTitle = IntPtr.Zero;
internal int dwX = 0;
internal int dwY = 0;
internal int dwXSize = 0;
internal int dwYSize = 0;
internal int dwXCountChars = 0;
internal int dwYCountChars = 0;
internal int dwFillAttribute = 0;
internal int dwFlags = 0;
internal short wShowWindow = 0;
internal short cbReserved2 = 0;
internal IntPtr lpReserved2 = IntPtr.Zero;
internal SafeFileHandle hStdInput = new SafeFileHandle(IntPtr.Zero, false);
internal SafeFileHandle hStdOutput = new SafeFileHandle(IntPtr.Zero, false);
internal SafeFileHandle hStdError = new SafeFileHandle(IntPtr.Zero, false);

internal
STARTUPINFO()
{
cb = Marshal.SizeOf<STARTUPINFO>();
}

public void Dispose()
{
// close the handles created for child process
if (hStdInput != null && !hStdInput.IsInvalid)
{
hStdInput.Dispose();
hStdInput = null;
}

if (hStdOutput != null && !hStdOutput.IsInvalid)
{
hStdOutput.Dispose();
hStdOutput = null;
}

if (hStdError != null && !hStdError.IsInvalid)
{
hStdError.Dispose();
hStdError = null;
}
}
internal IntPtr lpReserved;
internal IntPtr lpDesktop;
internal IntPtr lpTitle;
internal int dwX;
internal int dwY;
internal int dwXSize;
internal int dwYSize;
internal int dwXCountChars;
internal int dwYCountChars;
internal int dwFillAttribute;
internal int dwFlags;
internal short wShowWindow;
internal short cbReserved2;
internal IntPtr lpReserved2;
internal IntPtr hStdInput;
internal IntPtr hStdOutput;
internal IntPtr hStdError;
}
}
}
Loading

0 comments on commit 328fb66

Please sign in to comment.