void CreateProcess() { bool spawnNew = !string.IsNullOrEmpty(_options.DumpFolderForNewlyStartedProcess); _processName = null; int pid; if (int.TryParse(_options.ProcessInfo, out pid)) { _pid = pid; } else { // not numeric - let's try to find it by name var procs = Process.GetProcesses(); foreach (var proc in procs) { try { if (_options.ProcessInfo.Equals(proc.MainModule.ModuleName, StringComparison.OrdinalIgnoreCase)) { _pid = proc.Id; break; } } catch { // just ignore it } } } if (_pid > 0) { // process found - let's attach to it if (!DebuggingNativeMethods.DebugActiveProcess(_pid)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } _processName = GetProcessName(_pid); return; } if (spawnNew) { // final try - let's try creating it (but only if -x option is set) var commandLine = _options.ProcessInfo + " " + string.Join(" ", _options.Args ?? new string[0]); var startupInfo = new STARTUPINFO(); var processInformation = new PROCESS_INFORMATION(); var processCreationFlags = ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS; if (_options.StartProcessInNewConsoleWindow) { processCreationFlags |= ProcessCreationFlags.CREATE_NEW_CONSOLE; } bool res = ProcessNativeMethods.CreateProcess(null, new StringBuilder(commandLine), null, null, false, processCreationFlags, IntPtr.Zero, null, startupInfo, processInformation); if (!res) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } if (!DebuggingNativeMethods.DebugSetProcessKillOnExit(false)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } _pid = processInformation.dwProcessId; _processName = GetProcessName(_pid); return; } throw new ArgumentException("Something is wrong with the arguments - couldn't find or create a requested process."); }