private ProcessInformation CreateProcessWithStartInfo(SimpleProcessStartupInfo simpleProcessStartupInfo, CreateProcessOptions options) { Logger.LogInfo("CreateProcessWithStartInfo: Entry point."); var stringBuilder = BuildCommandLine(simpleProcessStartupInfo.FileName, simpleProcessStartupInfo.Arguments); Logger.LogInfo("CreateProcessWithStartInfo: command line is {0}.", stringBuilder); using (var startupInfo = new STARTUPINFO()) { Logger.LogInfo("CreateProcessWithStartInfo: Creation flags."); ProcessCreationFlags processCreationFlags = 0; if (simpleProcessStartupInfo.CreateNoWindow) { processCreationFlags |= ProcessCreationFlags.CREATE_NO_WINDOW; } if ((options & CreateProcessOptions.BreakAwayFromJob) != 0) { processCreationFlags |= ProcessCreationFlags.CREATE_BREAKAWAY_FROM_JOB; } var workingDirectory = simpleProcessStartupInfo.WorkingDirectory; if (workingDirectory == string.Empty) { workingDirectory = Environment.CurrentDirectory; } Logger.LogInfo("CreateProcessWithStartInfo: Working directory: {0}.", workingDirectory); if ((options & CreateProcessOptions.AttachDebugger) != 0) { Logger.LogInfo("CreateProcessWithStartInfo: Setting DEBUG_PROCESS flag."); processCreationFlags |= ProcessCreationFlags.DEBUG_PROCESS; processCreationFlags |= ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS; } Logger.LogInfo("CreateProcessWithStartInfo: Calling Win32 CreateProcess."); var processInformation = new PROCESS_INFORMATION(); var environmentPtr = IntPtr.Zero; var lastError = 0; var success = NativeMethods.CreateProcess(null, stringBuilder, null, null, true, processCreationFlags, environmentPtr, workingDirectory, startupInfo, processInformation); Logger.LogInfo("CreateProcessWithStartInfo: CreateProcess result: Success={0}-LastError={1}.", success, Marshal.GetLastWin32Error()); if (!success) { lastError = Marshal.GetLastWin32Error(); } // Assign safe handles as quickly as possible to avoid leaks. var safeProcessHandle = new SafeProcessHandle(processInformation.hProcess); var safeThreadHandle = new SafeProcessHandle(processInformation.hThread); if (!success) { throw new LastWin32ErrorException(lastError, string.Format("Error creating process from file \"{0}\"", simpleProcessStartupInfo.FileName)); } if (safeProcessHandle.IsInvalid || safeThreadHandle.IsInvalid) { Logger.LogInfo("CreateProcessWithStartInfo: Invalid process handle."); throw new Exception(string.Format("Error creating process from file \"{0}\" (invalid process handle)", simpleProcessStartupInfo.FileName)); } Logger.LogInfo("CreateProcessWithStartInfo: Creating ProcessResult instance."); var processResult = new ProcessInformation { ProcessHandle = safeProcessHandle, ProcessId = processInformation.dwProcessId }; safeThreadHandle.Close(); Logger.LogInfo("CreateProcessWithStartInfo: Success!"); return processResult; } }
private ProcessInformation CreateProcessImpl(string filename, string arguments, CreateProcessOptions options) { var info = new SimpleProcessStartupInfo { FileName = filename, Arguments = arguments, CreateNoWindow = true, WorkingDirectory = Path.GetDirectoryName(filename) }; return CreateProcessWithStartInfo(info, options); }
public CreateProcessResult CreateProcess(string filename, string arguments, CreateProcessOptions options) { Logger.LogInfo("CreateProcess: {0} {1}", filename, arguments); // Attaching the debugger ensures the child process will die with the current process. var debuggerObject = new DebuggerObject(); var processInformation = (options & CreateProcessOptions.AttachDebugger) != 0 ? debuggerObject.CreateProcess(() => CreateProcessImpl(filename, arguments, options)) : CreateProcessImpl(filename, arguments, options); Logger.LogInfo("CreateProcess: Creating CreateProcessResult instance."); return new CreateProcessResult(processInformation, debuggerObject); }
public CreateProcessResult CreateProcess(string filename, string arguments, CreateProcessOptions options) { Logger.LogInfo("CreateProcess: {0} {1}", filename, arguments); // Attaching the debugger ensures the child process will die with the current process. var debuggerObject = new DebuggerObject(); var processInformation = (options & CreateProcessOptions.AttachDebugger) != 0 ? debuggerObject.CreateProcess(() => CreateProcessImpl(filename, arguments, options)) : CreateProcessImpl(filename, arguments, options); Logger.LogInfo("CreateProcess: Creating CreateProcessResult instance."); return(new CreateProcessResult(processInformation, debuggerObject)); }
private ProcessInformation CreateProcessWithStartInfo(SimpleProcessStartupInfo simpleProcessStartupInfo, CreateProcessOptions options) { Logger.LogInfo("CreateProcessWithStartInfo: Entry point."); var stringBuilder = BuildCommandLine(simpleProcessStartupInfo.FileName, simpleProcessStartupInfo.Arguments); Logger.LogInfo("CreateProcessWithStartInfo: command line is {0}.", stringBuilder); using (var startupInfo = new STARTUPINFO()) { Logger.LogInfo("CreateProcessWithStartInfo: Creation flags."); ProcessCreationFlags processCreationFlags = 0; if (simpleProcessStartupInfo.CreateNoWindow) { processCreationFlags |= ProcessCreationFlags.CREATE_NO_WINDOW; } if ((options & CreateProcessOptions.BreakAwayFromJob) != 0) { processCreationFlags |= ProcessCreationFlags.CREATE_BREAKAWAY_FROM_JOB; } var workingDirectory = simpleProcessStartupInfo.WorkingDirectory; if (workingDirectory == string.Empty) { workingDirectory = Environment.CurrentDirectory; } Logger.LogInfo("CreateProcessWithStartInfo: Working directory: {0}.", workingDirectory); if ((options & CreateProcessOptions.AttachDebugger) != 0) { Logger.LogInfo("CreateProcessWithStartInfo: Setting DEBUG_PROCESS flag."); processCreationFlags |= ProcessCreationFlags.DEBUG_PROCESS; processCreationFlags |= ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS; } Logger.LogInfo("CreateProcessWithStartInfo: Calling Win32 CreateProcess."); var processInformation = new PROCESS_INFORMATION(); var environmentPtr = IntPtr.Zero; var lastError = 0; var success = NativeMethods.CreateProcess(null, stringBuilder, null, null, true, processCreationFlags, environmentPtr, workingDirectory, startupInfo, processInformation); Logger.LogInfo("CreateProcessWithStartInfo: CreateProcess result: Success={0}-LastError={1}.", success, Marshal.GetLastWin32Error()); if (!success) { lastError = Marshal.GetLastWin32Error(); } // Assign safe handles as quickly as possible to avoid leaks. var safeProcessHandle = new SafeProcessHandle(processInformation.hProcess); var safeThreadHandle = new SafeProcessHandle(processInformation.hThread); if (!success) { throw new LastWin32ErrorException(lastError, string.Format("Error creating process from file \"{0}\"", simpleProcessStartupInfo.FileName)); } if (safeProcessHandle.IsInvalid || safeThreadHandle.IsInvalid) { Logger.LogInfo("CreateProcessWithStartInfo: Invalid process handle."); throw new Exception(string.Format("Error creating process from file \"{0}\" (invalid process handle)", simpleProcessStartupInfo.FileName)); } Logger.LogInfo("CreateProcessWithStartInfo: Creating ProcessResult instance."); var processResult = new ProcessInformation { ProcessHandle = safeProcessHandle, ProcessId = processInformation.dwProcessId }; safeThreadHandle.Close(); Logger.LogInfo("CreateProcessWithStartInfo: Success!"); return(processResult); } }
private ProcessInformation CreateProcessImpl(string filename, string arguments, CreateProcessOptions options) { var info = new SimpleProcessStartupInfo { FileName = filename, Arguments = arguments, CreateNoWindow = true, WorkingDirectory = Path.GetDirectoryName(filename) }; return(CreateProcessWithStartInfo(info, options)); }