public extern static bool CreateProcess(string lpApplicationName, StringBuilder lpCommandLine, NativeMethods.SECURITY_ATTRIBUTES lpProcessAttributes, NativeMethods.SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, int dwCreationFlags, HandleRef lpEnvironment, string lpCurrentDirectory, NativeMethods.STARTUPINFO lpStartupInfo, NativeMethods.PROCESS_INFORMATION lpProcessInformation);
/// <include file='doc\Executor.uex' path='docs/doc[@for="Executor.ExecWaitWithCapture3"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> internal static int ExecWaitWithCapture(IntPtr userToken, string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName, string trueCmdLine) { IntSecurity.UnmanagedCode.Demand(); IntPtr output; IntPtr error; int retValue = 0; if (outputName == null || outputName.Length == 0) outputName = tempFiles.AddExtension("out"); if (errorName == null || errorName.Length == 0) errorName = tempFiles.AddExtension("err"); // Create the files output = CreateInheritedFile(outputName); error = CreateInheritedFile(errorName); bool success = false; NativeMethods.PROCESS_INFORMATION pi = new NativeMethods.PROCESS_INFORMATION(); IntPtr primaryToken = IntPtr.Zero; GCHandle environmentHandle = new GCHandle(); try { // Output the command line... // Make sure the FileStream doesn't own the handle FileStream outputStream = new FileStream(output, FileAccess.ReadWrite, false /*ownsHandle*/); StreamWriter sw = new StreamWriter(outputStream, Encoding.UTF8); sw.Write(currentDir); sw.Write("> "); // 'true' command line is used in case the command line points to // a response file sw.WriteLine(trueCmdLine != null ? trueCmdLine : cmd); sw.WriteLine(); sw.WriteLine(); sw.Flush(); outputStream.Close(); NativeMethods.STARTUPINFO si = new NativeMethods.STARTUPINFO(); si.cb = Marshal.SizeOf(si); si.dwFlags = NativeMethods.STARTF_USESTDHANDLES; si.hStdOutput = output; si.hStdError = error; si.hStdInput = UnsafeNativeMethods.GetStdHandle(NativeMethods.STD_INPUT_HANDLE); // // Prepare the environment // Hashtable environment = new Hashtable(); // Add the current environment foreach (DictionaryEntry entry in Environment.GetEnvironmentVariables()) environment.Add((string)entry.Key, (string)entry.Value); // Add the flag to indicate restricted security in the process environment.Add("_ClrRestrictSecAttributes", "1"); // set up the environment block parameter IntPtr environmentPtr = (IntPtr)0; byte[] environmentBytes = EnvironmentToByteArray(environment); environmentHandle = GCHandle.Alloc(environmentBytes, GCHandleType.Pinned); environmentPtr = environmentHandle.AddrOfPinnedObject(); if (userToken == IntPtr.Zero) { success = UnsafeNativeMethods.CreateProcess( null, // String lpApplicationName, new StringBuilder(cmd), // String lpCommandLine, null, // SECURITY_ATTRIBUTES lpProcessAttributes, null, // SECURITY_ATTRIBUTES lpThreadAttributes, true, // bool bInheritHandles, 0, // int dwCreationFlags, environmentPtr, // int lpEnvironment, currentDir, // String lpCurrentDirectory, si, // STARTUPINFO lpStartupInfo, pi); // PROCESS_INFORMATION lpProcessInformation); } else { throw new NotSupportedException(); } } finally { // free environment block if (environmentHandle.IsAllocated) environmentHandle.Free(); // Close the file handles UnsafeNativeMethods.CloseHandle(output); UnsafeNativeMethods.CloseHandle(error); } if (success) { try { int ret = SafeNativeMethods.WaitForSingleObject(pi.hProcess, ProcessTimeOut); // Check for timeout if (ret == NativeMethods.WAIT_TIMEOUT) { throw new ExternalException(SR.GetString(SR.ExecTimeout, cmd), NativeMethods.WAIT_TIMEOUT); } if (ret != NativeMethods.WAIT_OBJECT_0) { throw new ExternalException(SR.GetString(SR.ExecBadreturn, cmd), Marshal.GetLastWin32Error()); } // Check the process's exit code int status = NativeMethods.STILL_ACTIVE; if (!UnsafeNativeMethods.GetExitCodeProcess(pi.hProcess, ref status)) { throw new ExternalException(SR.GetString(SR.ExecCantGetRetCode, cmd), Marshal.GetLastWin32Error()); } retValue = status; } finally { UnsafeNativeMethods.CloseHandle(pi.hThread); UnsafeNativeMethods.CloseHandle(pi.hProcess); if (primaryToken != IntPtr.Zero) UnsafeNativeMethods.CloseHandle(primaryToken); } } else { throw new ExternalException(SR.GetString(SR.ExecCantExec, cmd), Marshal.GetLastWin32Error()); } return retValue; }