private SafeFileHandle GetSafeFileHandleForRedirection(string RedirectionPath, int dwCreationDisposition) { ProcessNativeMethods.SECURITY_ATTRIBUTES sECURITYATTRIBUTE = new ProcessNativeMethods.SECURITY_ATTRIBUTES(); IntPtr intPtr = ProcessNativeMethods.CreateFileW(RedirectionPath, ProcessNativeMethods.GENERIC_READ | ProcessNativeMethods.GENERIC_WRITE, ProcessNativeMethods.FILE_SHARE_WRITE | ProcessNativeMethods.FILE_SHARE_READ, sECURITYATTRIBUTE, dwCreationDisposition, ProcessNativeMethods.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if (intPtr == IntPtr.Zero) { int lastWin32Error = Marshal.GetLastWin32Error(); Win32Exception win32Exception = new Win32Exception(lastWin32Error); string str = StringUtil.Format(ProcessResources.InvalidStartProcess, win32Exception.Message); ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.ThrowTerminatingError(errorRecord); } SafeFileHandle safeFileHandle = new SafeFileHandle(intPtr, true); return(safeFileHandle); }
public static extern bool CreateProcess(string lpApplicationName, StringBuilder lpCommandLine, ProcessNativeMethods.SECURITY_ATTRIBUTES lpProcessAttributes, ProcessNativeMethods.SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, int dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ProcessNativeMethods.STARTUPINFO lpStartupInfo, SafeNativeMethods.PROCESS_INFORMATION lpProcessInformation);
public static extern IntPtr CreateFileW(string lpFileName, int dwDesiredAccess, int dwShareMode, ProcessNativeMethods.SECURITY_ATTRIBUTES lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
private int StartWithCreateProcess(ProcessStartInfo startinfo) { string str; bool flag; ProcessNativeMethods.STARTUPINFO sTARTUPINFO = new ProcessNativeMethods.STARTUPINFO(); SafeNativeMethods.PROCESS_INFORMATION pROCESSINFORMATION = new SafeNativeMethods.PROCESS_INFORMATION(); int lastWin32Error = 0; GCHandle gCHandle = new GCHandle(); StringBuilder stringBuilder = StartProcessCommand.BuildCommandLine(startinfo.FileName, startinfo.Arguments); try { if (this._redirectstandardinput == null) { sTARTUPINFO.hStdInput = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-10), false); } else { startinfo.RedirectStandardInput = true; this._redirectstandardinput = this.ResolveFilePath(this._redirectstandardinput); sTARTUPINFO.hStdInput = this.GetSafeFileHandleForRedirection(this._redirectstandardinput, ProcessNativeMethods.OPEN_EXISTING); } if (this._redirectstandardoutput == null) { sTARTUPINFO.hStdOutput = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-11), false); } else { startinfo.RedirectStandardOutput = true; this._redirectstandardoutput = this.ResolveFilePath(this._redirectstandardoutput); sTARTUPINFO.hStdOutput = this.GetSafeFileHandleForRedirection(this._redirectstandardoutput, ProcessNativeMethods.CREATE_ALWAYS); } if (this._redirectstandarderror == null) { sTARTUPINFO.hStdError = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-12), false); } else { startinfo.RedirectStandardError = true; this._redirectstandarderror = this.ResolveFilePath(this._redirectstandarderror); sTARTUPINFO.hStdError = this.GetSafeFileHandleForRedirection(this._redirectstandarderror, ProcessNativeMethods.CREATE_ALWAYS); } sTARTUPINFO.dwFlags = 0x100; int num = 0; if (!startinfo.CreateNoWindow) { num = num | 16; ProcessNativeMethods.STARTUPINFO sTARTUPINFO1 = sTARTUPINFO; sTARTUPINFO1.dwFlags = sTARTUPINFO1.dwFlags | 1; ProcessWindowStyle windowStyle = startinfo.WindowStyle; switch (windowStyle) { case ProcessWindowStyle.Normal: { sTARTUPINFO.wShowWindow = 1; break; } case ProcessWindowStyle.Hidden: { sTARTUPINFO.wShowWindow = 0; break; } case ProcessWindowStyle.Minimized: { sTARTUPINFO.wShowWindow = 2; break; } case ProcessWindowStyle.Maximized: { sTARTUPINFO.wShowWindow = 3; break; } } } else { num = 0; } IntPtr zero = IntPtr.Zero; if (startinfo.EnvironmentVariables != null && this.UseNewEnvironment) { bool flag1 = false; if (StartProcessCommand.ProcessManager.IsNt) { num = num | 0x400; flag1 = true; } gCHandle = GCHandle.Alloc(StartProcessCommand.EnvironmentBlock.ToByteArray(startinfo.EnvironmentVariables, flag1), GCHandleType.Pinned); zero = gCHandle.AddrOfPinnedObject(); } if (this._credential == null) { ProcessNativeMethods.SECURITY_ATTRIBUTES sECURITYATTRIBUTE = new ProcessNativeMethods.SECURITY_ATTRIBUTES(); ProcessNativeMethods.SECURITY_ATTRIBUTES sECURITYATTRIBUTE1 = new ProcessNativeMethods.SECURITY_ATTRIBUTES(); flag = ProcessNativeMethods.CreateProcess(null, stringBuilder, sECURITYATTRIBUTE, sECURITYATTRIBUTE1, true, num, zero, startinfo.WorkingDirectory, sTARTUPINFO, pROCESSINFORMATION); if (!flag) { lastWin32Error = Marshal.GetLastWin32Error(); } if (!flag) { Win32Exception win32Exception = new Win32Exception(lastWin32Error); str = StringUtil.Format(ProcessResources.InvalidStartProcess, win32Exception.Message); ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.ThrowTerminatingError(errorRecord); } } else { ProcessNativeMethods.LogonFlags logonFlag = 0; if (startinfo.LoadUserProfile) { logonFlag = ProcessNativeMethods.LogonFlags.LOGON_WITH_PROFILE; } IntPtr coTaskMemUnicode = IntPtr.Zero; try { if (startinfo.Password != null) { coTaskMemUnicode = Marshal.SecureStringToCoTaskMemUnicode(startinfo.Password); } else { coTaskMemUnicode = Marshal.StringToCoTaskMemUni(string.Empty); } flag = ProcessNativeMethods.CreateProcessWithLogonW(startinfo.UserName, startinfo.Domain, coTaskMemUnicode, logonFlag, null, stringBuilder, num, zero, startinfo.WorkingDirectory, sTARTUPINFO, pROCESSINFORMATION); if (!flag) { lastWin32Error = Marshal.GetLastWin32Error(); } if (!flag) { if (lastWin32Error == 193) { str = StringUtil.Format(ProcessResources.InvalidApplication, this._path); ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.ThrowTerminatingError(errorRecord1); } Win32Exception win32Exception1 = new Win32Exception(lastWin32Error); str = StringUtil.Format(ProcessResources.InvalidStartProcess, win32Exception1.Message); ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.ThrowTerminatingError(errorRecord2); } } finally { if (coTaskMemUnicode != IntPtr.Zero) { Marshal.ZeroFreeCoTaskMemUnicode(coTaskMemUnicode); } } } } finally { if (gCHandle.IsAllocated) { gCHandle.Free(); } sTARTUPINFO.Dispose(); } return pROCESSINFORMATION.dwProcessId; }
private SafeFileHandle GetSafeFileHandleForRedirection(string RedirectionPath, int dwCreationDisposition) { ProcessNativeMethods.SECURITY_ATTRIBUTES sECURITYATTRIBUTE = new ProcessNativeMethods.SECURITY_ATTRIBUTES(); IntPtr intPtr = ProcessNativeMethods.CreateFileW(RedirectionPath, ProcessNativeMethods.GENERIC_READ | ProcessNativeMethods.GENERIC_WRITE, ProcessNativeMethods.FILE_SHARE_WRITE | ProcessNativeMethods.FILE_SHARE_READ, sECURITYATTRIBUTE, dwCreationDisposition, ProcessNativeMethods.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if (intPtr == IntPtr.Zero) { int lastWin32Error = Marshal.GetLastWin32Error(); Win32Exception win32Exception = new Win32Exception(lastWin32Error); string str = StringUtil.Format(ProcessResources.InvalidStartProcess, win32Exception.Message); ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.ThrowTerminatingError(errorRecord); } SafeFileHandle safeFileHandle = new SafeFileHandle(intPtr, true); return safeFileHandle; }
private SafeFileHandle GetSafeFileHandleForRedirection(string RedirectionPath, uint dwCreationDisposition) { System.IntPtr hFileHandle = System.IntPtr.Zero; ProcessNativeMethods.SECURITY_ATTRIBUTES lpSecurityAttributes = new ProcessNativeMethods.SECURITY_ATTRIBUTES(); hFileHandle = ProcessNativeMethods.CreateFileW(RedirectionPath, ProcessNativeMethods.GENERIC_READ | ProcessNativeMethods.GENERIC_WRITE, ProcessNativeMethods.FILE_SHARE_WRITE | ProcessNativeMethods.FILE_SHARE_READ, lpSecurityAttributes, dwCreationDisposition, ProcessNativeMethods.FILE_ATTRIBUTE_NORMAL, System.IntPtr.Zero); if (hFileHandle == System.IntPtr.Zero) { int error = Marshal.GetLastWin32Error(); Win32Exception win32ex = new Win32Exception(error); string message = StringUtil.Format(ProcessResources.InvalidStartProcess, win32ex.Message); ErrorRecord er = new ErrorRecord(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, null); ThrowTerminatingError(er); } SafeFileHandle sf = new SafeFileHandle(hFileHandle, true); return sf; }
private Process StartWithCreateProcess(ProcessStartInfo startinfo) { ProcessNativeMethods.STARTUPINFO lpStartupInfo = new ProcessNativeMethods.STARTUPINFO(); SafeNativeMethods.PROCESS_INFORMATION lpProcessInformation = new SafeNativeMethods.PROCESS_INFORMATION(); int error = 0; GCHandle pinnedEnvironmentBlock = new GCHandle(); string message = String.Empty; //building the cmdline with the file name given and it's arguments StringBuilder cmdLine = BuildCommandLine(startinfo.FileName, startinfo.Arguments); try { //RedirectionStandardInput if (_redirectstandardinput != null) { startinfo.RedirectStandardInput = true; _redirectstandardinput = ResolveFilePath(_redirectstandardinput); lpStartupInfo.hStdInput = GetSafeFileHandleForRedirection(_redirectstandardinput, ProcessNativeMethods.OPEN_EXISTING); } else { lpStartupInfo.hStdInput = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-10), false); } //RedirectionStandardOutput if (_redirectstandardoutput != null) { startinfo.RedirectStandardOutput = true; _redirectstandardoutput = ResolveFilePath(_redirectstandardoutput); lpStartupInfo.hStdOutput = GetSafeFileHandleForRedirection(_redirectstandardoutput, ProcessNativeMethods.CREATE_ALWAYS); } else { lpStartupInfo.hStdOutput = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-11), false); } //RedirectionStandardError if (_redirectstandarderror != null) { startinfo.RedirectStandardError = true; _redirectstandarderror = ResolveFilePath(_redirectstandarderror); lpStartupInfo.hStdError = GetSafeFileHandleForRedirection(_redirectstandarderror, ProcessNativeMethods.CREATE_ALWAYS); } else { lpStartupInfo.hStdError = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-12), false); } //STARTF_USESTDHANDLES lpStartupInfo.dwFlags = 0x100; int creationFlags = 0; if (startinfo.CreateNoWindow) { //No new window: Inherit the parent process's console window creationFlags = 0x00000000; } else { //CREATE_NEW_CONSOLE creationFlags |= 0x00000010; //STARTF_USESHOWWINDOW lpStartupInfo.dwFlags |= 0x00000001; #if CORECLR //SW_SHOWNORMAL lpStartupInfo.wShowWindow = 1; #else switch (startinfo.WindowStyle) { case ProcessWindowStyle.Normal: //SW_SHOWNORMAL lpStartupInfo.wShowWindow = 1; break; case ProcessWindowStyle.Minimized: //SW_SHOWMINIMIZED lpStartupInfo.wShowWindow = 2; break; case ProcessWindowStyle.Maximized: //SW_SHOWMAXIMIZED lpStartupInfo.wShowWindow = 3; break; case ProcessWindowStyle.Hidden: //SW_HIDE lpStartupInfo.wShowWindow = 0; break; } #endif } // Create the new process suspended so we have a chance to get a corresponding Process object in case it terminates quickly. creationFlags |= 0x00000004; IntPtr AddressOfEnvironmentBlock = IntPtr.Zero; var environmentVars = ClrFacade.GetProcessEnvironment(startinfo); if (environmentVars != null) { if (this.UseNewEnvironment) { bool unicode = false; if (ProcessManager.IsNt) { creationFlags |= 0x400; unicode = true; } pinnedEnvironmentBlock = GCHandle.Alloc(EnvironmentBlock.ToByteArray(environmentVars, unicode), GCHandleType.Pinned); AddressOfEnvironmentBlock = pinnedEnvironmentBlock.AddrOfPinnedObject(); } } bool flag; if (_credential != null) { ProcessNativeMethods.LogonFlags logonFlags = 0; if (startinfo.LoadUserProfile) { logonFlags = ProcessNativeMethods.LogonFlags.LOGON_WITH_PROFILE; } IntPtr password = IntPtr.Zero; try { #if CORECLR password = (startinfo.PasswordInClearText == null) ? Marshal.StringToCoTaskMemUni(string.Empty) : Marshal.StringToCoTaskMemUni(startinfo.PasswordInClearText); #else password = (startinfo.Password == null) ? Marshal.StringToCoTaskMemUni(string.Empty) : ClrFacade.SecureStringToCoTaskMemUnicode(startinfo.Password); #endif flag = ProcessNativeMethods.CreateProcessWithLogonW(startinfo.UserName, startinfo.Domain, password, logonFlags, null, cmdLine, creationFlags, AddressOfEnvironmentBlock, startinfo.WorkingDirectory, lpStartupInfo, lpProcessInformation); if (!flag) { error = Marshal.GetLastWin32Error(); ErrorRecord er = null; if (error == 0xc1) { message = StringUtil.Format(ProcessResources.InvalidApplication, FilePath); } else if (error == 0x424) { // The API 'CreateProcessWithLogonW' depends on the 'Secondary Logon' service, but the component 'Microsoft-Windows-SecondaryLogonService' // is not installed in OneCoreUAP. We will get error code 0x424 when the service is not available. message = StringUtil.Format(ProcessResources.ParameterNotSupported, "-Credential", "Start-Process"); er = new ErrorRecord(new NotSupportedException(message), "NotSupportedException", ErrorCategory.NotInstalled, null); } else { Win32Exception win32ex = new Win32Exception(error); message = StringUtil.Format(ProcessResources.InvalidStartProcess, win32ex.Message); } er = er ?? new ErrorRecord(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, null); ThrowTerminatingError(er); } goto Label_03AE; } finally { if (password != IntPtr.Zero) { Marshal.ZeroFreeCoTaskMemUnicode(password); } } }//end of if ProcessNativeMethods.SECURITY_ATTRIBUTES lpProcessAttributes = new ProcessNativeMethods.SECURITY_ATTRIBUTES(); ProcessNativeMethods.SECURITY_ATTRIBUTES lpThreadAttributes = new ProcessNativeMethods.SECURITY_ATTRIBUTES(); flag = ProcessNativeMethods.CreateProcess(null, cmdLine, lpProcessAttributes, lpThreadAttributes, true, creationFlags, AddressOfEnvironmentBlock, startinfo.WorkingDirectory, lpStartupInfo, lpProcessInformation); if (!flag) { error = Marshal.GetLastWin32Error(); Win32Exception win32ex = new Win32Exception(error); message = StringUtil.Format(ProcessResources.InvalidStartProcess, win32ex.Message); ErrorRecord er = new ErrorRecord(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, null); ThrowTerminatingError(er); } Label_03AE: // At this point, we should have a suspended process. Get the .Net Process object, resume the process, and return. Process result = Process.GetProcessById(lpProcessInformation.dwProcessId); ProcessNativeMethods.ResumeThread(lpProcessInformation.hThread); return result; } finally { if (pinnedEnvironmentBlock.IsAllocated) { pinnedEnvironmentBlock.Free(); } lpStartupInfo.Dispose(); lpProcessInformation.Dispose(); } }
private int StartWithCreateProcess(ProcessStartInfo startinfo) { string str; bool flag; ProcessNativeMethods.STARTUPINFO sTARTUPINFO = new ProcessNativeMethods.STARTUPINFO(); SafeNativeMethods.PROCESS_INFORMATION pROCESSINFORMATION = new SafeNativeMethods.PROCESS_INFORMATION(); int lastWin32Error = 0; GCHandle gCHandle = new GCHandle(); StringBuilder stringBuilder = StartProcessCommand.BuildCommandLine(startinfo.FileName, startinfo.Arguments); try { if (this._redirectstandardinput == null) { sTARTUPINFO.hStdInput = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-10), false); } else { startinfo.RedirectStandardInput = true; this._redirectstandardinput = this.ResolveFilePath(this._redirectstandardinput); sTARTUPINFO.hStdInput = this.GetSafeFileHandleForRedirection(this._redirectstandardinput, ProcessNativeMethods.OPEN_EXISTING); } if (this._redirectstandardoutput == null) { sTARTUPINFO.hStdOutput = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-11), false); } else { startinfo.RedirectStandardOutput = true; this._redirectstandardoutput = this.ResolveFilePath(this._redirectstandardoutput); sTARTUPINFO.hStdOutput = this.GetSafeFileHandleForRedirection(this._redirectstandardoutput, ProcessNativeMethods.CREATE_ALWAYS); } if (this._redirectstandarderror == null) { sTARTUPINFO.hStdError = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-12), false); } else { startinfo.RedirectStandardError = true; this._redirectstandarderror = this.ResolveFilePath(this._redirectstandarderror); sTARTUPINFO.hStdError = this.GetSafeFileHandleForRedirection(this._redirectstandarderror, ProcessNativeMethods.CREATE_ALWAYS); } sTARTUPINFO.dwFlags = 0x100; int num = 0; if (!startinfo.CreateNoWindow) { num = num | 16; ProcessNativeMethods.STARTUPINFO sTARTUPINFO1 = sTARTUPINFO; sTARTUPINFO1.dwFlags = sTARTUPINFO1.dwFlags | 1; ProcessWindowStyle windowStyle = startinfo.WindowStyle; switch (windowStyle) { case ProcessWindowStyle.Normal: { sTARTUPINFO.wShowWindow = 1; break; } case ProcessWindowStyle.Hidden: { sTARTUPINFO.wShowWindow = 0; break; } case ProcessWindowStyle.Minimized: { sTARTUPINFO.wShowWindow = 2; break; } case ProcessWindowStyle.Maximized: { sTARTUPINFO.wShowWindow = 3; break; } } } else { num = 0; } IntPtr zero = IntPtr.Zero; if (startinfo.EnvironmentVariables != null && this.UseNewEnvironment) { bool flag1 = false; if (StartProcessCommand.ProcessManager.IsNt) { num = num | 0x400; flag1 = true; } gCHandle = GCHandle.Alloc(StartProcessCommand.EnvironmentBlock.ToByteArray(startinfo.EnvironmentVariables, flag1), GCHandleType.Pinned); zero = gCHandle.AddrOfPinnedObject(); } if (this._credential == null) { ProcessNativeMethods.SECURITY_ATTRIBUTES sECURITYATTRIBUTE = new ProcessNativeMethods.SECURITY_ATTRIBUTES(); ProcessNativeMethods.SECURITY_ATTRIBUTES sECURITYATTRIBUTE1 = new ProcessNativeMethods.SECURITY_ATTRIBUTES(); flag = ProcessNativeMethods.CreateProcess(null, stringBuilder, sECURITYATTRIBUTE, sECURITYATTRIBUTE1, true, num, zero, startinfo.WorkingDirectory, sTARTUPINFO, pROCESSINFORMATION); if (!flag) { lastWin32Error = Marshal.GetLastWin32Error(); } if (!flag) { Win32Exception win32Exception = new Win32Exception(lastWin32Error); str = StringUtil.Format(ProcessResources.InvalidStartProcess, win32Exception.Message); ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.ThrowTerminatingError(errorRecord); } } else { ProcessNativeMethods.LogonFlags logonFlag = 0; if (startinfo.LoadUserProfile) { logonFlag = ProcessNativeMethods.LogonFlags.LOGON_WITH_PROFILE; } IntPtr coTaskMemUnicode = IntPtr.Zero; try { if (startinfo.Password != null) { coTaskMemUnicode = Marshal.SecureStringToCoTaskMemUnicode(startinfo.Password); } else { coTaskMemUnicode = Marshal.StringToCoTaskMemUni(string.Empty); } flag = ProcessNativeMethods.CreateProcessWithLogonW(startinfo.UserName, startinfo.Domain, coTaskMemUnicode, logonFlag, null, stringBuilder, num, zero, startinfo.WorkingDirectory, sTARTUPINFO, pROCESSINFORMATION); if (!flag) { lastWin32Error = Marshal.GetLastWin32Error(); } if (!flag) { if (lastWin32Error == 193) { str = StringUtil.Format(ProcessResources.InvalidApplication, this._path); ErrorRecord errorRecord1 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.ThrowTerminatingError(errorRecord1); } Win32Exception win32Exception1 = new Win32Exception(lastWin32Error); str = StringUtil.Format(ProcessResources.InvalidStartProcess, win32Exception1.Message); ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.ThrowTerminatingError(errorRecord2); } } finally { if (coTaskMemUnicode != IntPtr.Zero) { Marshal.ZeroFreeCoTaskMemUnicode(coTaskMemUnicode); } } } } finally { if (gCHandle.IsAllocated) { gCHandle.Free(); } sTARTUPINFO.Dispose(); } return(pROCESSINFORMATION.dwProcessId); }