public static extern bool CreateProcessW( [MarshalAs(UnmanagedType.LPWStr)] string lpApplicationName, StringBuilder lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, NativeHelpers.ProcessCreationFlags dwCreationFlags, SafeMemoryBuffer lpEnvironment, [MarshalAs(UnmanagedType.LPWStr)] string lpCurrentDirectory, NativeHelpers.STARTUPINFOEX lpStartupInfo, out NativeHelpers.PROCESS_INFORMATION lpProcessInformation);
internal static void CreateStdioPipes(NativeHelpers.STARTUPINFOEX si, out SafeFileHandle stdoutRead, out SafeFileHandle stdoutWrite, out SafeFileHandle stderrRead, out SafeFileHandle stderrWrite, out SafeFileHandle stdinRead, out SafeFileHandle stdinWrite) { NativeHelpers.SECURITY_ATTRIBUTES pipesec = new NativeHelpers.SECURITY_ATTRIBUTES(); pipesec.bInheritHandle = true; if (!NativeMethods.CreatePipe(out stdoutRead, out stdoutWrite, pipesec, 0)) { throw new Win32Exception("STDOUT pipe setup failed"); } if (!NativeMethods.SetHandleInformation(stdoutRead, NativeHelpers.HandleFlags.INHERIT, 0)) { throw new Win32Exception("STDOUT pipe handle setup failed"); } if (!NativeMethods.CreatePipe(out stderrRead, out stderrWrite, pipesec, 0)) { throw new Win32Exception("STDERR pipe setup failed"); } if (!NativeMethods.SetHandleInformation(stderrRead, NativeHelpers.HandleFlags.INHERIT, 0)) { throw new Win32Exception("STDERR pipe handle setup failed"); } if (!NativeMethods.CreatePipe(out stdinRead, out stdinWrite, pipesec, 0)) { throw new Win32Exception("STDIN pipe setup failed"); } if (!NativeMethods.SetHandleInformation(stdinWrite, NativeHelpers.HandleFlags.INHERIT, 0)) { throw new Win32Exception("STDIN pipe handle setup failed"); } si.startupInfo.hStdOutput = stdoutWrite; si.startupInfo.hStdError = stderrWrite; si.startupInfo.hStdInput = stdinRead; }
/// <summary> /// Creates a process based on the CreateProcess API call. /// </summary> /// <param name="lpApplicationName">The name of the executable or batch file to execute</param> /// <param name="lpCommandLine">The command line to execute, typically this includes lpApplication as the first argument</param> /// <param name="lpCurrentDirectory">The full path to the current directory for the process, null will have the same cwd as the calling process</param> /// <param name="environment">A dictionary of key/value pairs to define the new process environment</param> /// <param name="stdin">A byte array to send over the stdin pipe</param> /// <param name="outputEncoding">The character encoding for decoding stdout/stderr output of the process.</param> /// <param name="waitChildren">Whether to wait for any children spawned by the process to finished (Server2012 +).</param> /// <returns>Result object that contains the command output and return code</returns> public static Result CreateProcess(string lpApplicationName, string lpCommandLine, string lpCurrentDirectory, IDictionary environment, byte[] stdin, string outputEncoding, bool waitChildren) { NativeHelpers.ProcessCreationFlags creationFlags = NativeHelpers.ProcessCreationFlags.CREATE_SUSPENDED | NativeHelpers.ProcessCreationFlags.CREATE_UNICODE_ENVIRONMENT | NativeHelpers.ProcessCreationFlags.EXTENDED_STARTUPINFO_PRESENT; NativeHelpers.PROCESS_INFORMATION pi = new NativeHelpers.PROCESS_INFORMATION(); NativeHelpers.STARTUPINFOEX si = new NativeHelpers.STARTUPINFOEX(); si.startupInfo.dwFlags = NativeHelpers.StartupInfoFlags.USESTDHANDLES; SafeFileHandle stdoutRead, stdoutWrite, stderrRead, stderrWrite, stdinRead, stdinWrite; CreateStdioPipes(si, out stdoutRead, out stdoutWrite, out stderrRead, out stderrWrite, out stdinRead, out stdinWrite); FileStream stdinStream = new FileStream(stdinWrite, FileAccess.Write); // $null from PowerShell ends up as an empty string, we need to convert back as an empty string doesn't // make sense for these parameters if (lpApplicationName == "") { lpApplicationName = null; } if (lpCurrentDirectory == "") { lpCurrentDirectory = null; } using (SafeMemoryBuffer lpEnvironment = CreateEnvironmentPointer(environment)) { // Create console with utf-8 CP if no existing console is present bool isConsole = false; if (NativeMethods.GetConsoleWindow() == IntPtr.Zero) { isConsole = NativeMethods.AllocConsole(); // Set console input/output codepage to UTF-8 NativeMethods.SetConsoleCP(65001); NativeMethods.SetConsoleOutputCP(65001); } try { StringBuilder commandLine = new StringBuilder(lpCommandLine); if (!NativeMethods.CreateProcessW(lpApplicationName, commandLine, IntPtr.Zero, IntPtr.Zero, true, creationFlags, lpEnvironment, lpCurrentDirectory, si, out pi)) { throw new Win32Exception("CreateProcessW() failed"); } } finally { if (isConsole) { NativeMethods.FreeConsole(); } } } return(WaitProcess(stdoutRead, stdoutWrite, stderrRead, stderrWrite, stdinStream, stdin, pi, outputEncoding, waitChildren)); }
/// <summary> /// Wrapper around the Win32 CreateProcess API for low level use. This just spawns the new process and does not /// wait until it is complete before returning. /// </summary> /// <param name="applicationName">The name of the executable or batch file to execute</param> /// <param name="commandLine">The command line to execute, typically this includes applicationName as the first argument</param> /// <param name="processAttributes">SecurityAttributes to assign to the new process, set to null to use the defaults</param> /// <param name="threadAttributes">SecurityAttributes to assign to the new thread, set to null to use the defaults</param> /// <param name="inheritHandles">Any inheritable handles in the calling process is inherited in the new process</param> /// <param name="creationFlags">Custom creation flags to use when creating the new process</param> /// <param name="environment">A dictionary of key/value pairs to define the new process environment</param> /// <param name="currentDirectory">The full path to the current directory for the process, null will have the same cwd as the calling process</param> /// <param name="startupInfo">Custom StartupInformation to use when creating the new process</param> /// <returns>ProcessInformation containing a handle to the process and main thread as well as the pid/tid.</returns> public static ProcessInformation NativeCreateProcess(string applicationName, string commandLine, SecurityAttributes processAttributes, SecurityAttributes threadAttributes, bool inheritHandles, ProcessCreationFlags creationFlags, IDictionary environment, string currentDirectory, StartupInfo startupInfo) { // We always have the extended version present. creationFlags |= ProcessCreationFlags.ExtendedStartupInfoPresent; // $null from PowerShell ends up as an empty string, we need to convert back as an empty string doesn't // make sense for these parameters if (String.IsNullOrWhiteSpace(applicationName)) { applicationName = null; } if (String.IsNullOrWhiteSpace(currentDirectory)) { currentDirectory = null; } NativeHelpers.STARTUPINFOEX si = new NativeHelpers.STARTUPINFOEX(); if (!String.IsNullOrWhiteSpace(startupInfo.Desktop)) { si.startupInfo.lpDesktop = startupInfo.Desktop; } if (!String.IsNullOrWhiteSpace(startupInfo.Title)) { si.startupInfo.lpTitle = startupInfo.Title; } bool useStdHandles = false; if (startupInfo.StandardInput != null) { si.startupInfo.hStdInput = startupInfo.StandardInput; useStdHandles = true; } if (startupInfo.StandardOutput != null) { si.startupInfo.hStdOutput = startupInfo.StandardOutput; useStdHandles = true; } if (startupInfo.StandardError != null) { si.startupInfo.hStdError = startupInfo.StandardError; useStdHandles = true; } if (useStdHandles) { si.startupInfo.dwFlags |= NativeHelpers.StartupInfoFlags.USESTDHANDLES; } if (startupInfo.WindowStyle != null) { switch (startupInfo.WindowStyle) { case ProcessWindowStyle.Normal: si.startupInfo.wShowWindow = 1; // SW_SHOWNORMAL break; case ProcessWindowStyle.Hidden: si.startupInfo.wShowWindow = 0; // SW_HIDE break; case ProcessWindowStyle.Minimized: si.startupInfo.wShowWindow = 6; // SW_MINIMIZE break; case ProcessWindowStyle.Maximized: si.startupInfo.wShowWindow = 3; // SW_MAXIMIZE break; } si.startupInfo.dwFlags |= NativeHelpers.StartupInfoFlags.STARTF_USESHOWWINDOW; } NativeHelpers.PROCESS_INFORMATION pi = new NativeHelpers.PROCESS_INFORMATION(); using (SafeMemoryBuffer lpProcessAttr = CreateSecurityAttributes(processAttributes)) using (SafeMemoryBuffer lpThreadAttributes = CreateSecurityAttributes(threadAttributes)) using (SafeMemoryBuffer lpEnvironment = CreateEnvironmentPointer(environment)) { StringBuilder commandLineBuff = new StringBuilder(commandLine); if (!NativeMethods.CreateProcessW(applicationName, commandLineBuff, lpProcessAttr, lpThreadAttributes, inheritHandles, creationFlags, lpEnvironment, currentDirectory, si, out pi)) { throw new Win32Exception("CreateProcessW() failed"); } } return(new ProcessInformation { Process = new SafeNativeHandle(pi.hProcess), Thread = new SafeNativeHandle(pi.hThread), ProcessId = pi.dwProcessId, ThreadId = pi.dwThreadId, }); }