private ProcessHandler(string filename, string arguments) { //Start debugging thread _activeThread = TaskHandler.RunTask(Debugger, filename, arguments); }
private void Debugger(object value) { _continue = true; var parameters = value as object[]; var filename = parameters[0] as string; var arguments = parameters[1] as string; if (!string.IsNullOrEmpty(arguments) && !arguments.StartsWith(" ")) { arguments = string.Concat(" ", arguments); } var si = new STARTUPINFO(); PROCESS_INFORMATION pi; //Create Process if (!CreateProcess(null, string.Format("\"{0}\"{1}", filename, arguments), IntPtr.Zero, IntPtr.Zero, true, ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS | ProcessCreationFlags.DEBUG_PROCESS, IntPtr.Zero, null, ref si, out pi)) { throw new Exception(string.Format("Failed to start {0}.", Path.GetFileName(filename))); } //if (!CreateProcess(null, string.Format("\"{0}\"{1}", filename, arguments), // IntPtr.Zero, IntPtr.Zero, false, // ProcessCreationFlags.NORMAL_PRIORITY_CLASS, // IntPtr.Zero, null, ref si, out pi)) // throw new Exception(string.Format("Failed to start {0}.", Path.GetFileName(filename))); //Open Process Handle _handle = OpenProcess(ProcessAccessPriviledges.PROCESS_VM_READ | ProcessAccessPriviledges.PROCESS_TERMINATE | ProcessAccessPriviledges.PROCESS_VM_WRITE | ProcessAccessPriviledges.PROCESS_VM_OPERATION, false, pi.dwProcessId); //Create debug event var debugEvent = new DEBUG_EVENT() { dwProcessId = pi.dwProcessId, dwThreadId = pi.dwThreadId }; try { do { //DebugDetector.AssertCheckRunning(); if (WaitForDebugEvent(ref debugEvent, 100)) { switch (debugEvent.dwDebugEventCode) { //Continue case DebugEventTypes.EXCEPTION_DEBUG_EVENT: case DebugEventTypes.CREATE_THREAD_DEBUG_EVENT: case DebugEventTypes.LOAD_DLL_DEBUG_EVENT: case DebugEventTypes.UNLOAD_DLL_DEBUG_EVENT: case DebugEventTypes.OUTPUT_DEBUG_STRING_EVENT: case DebugEventTypes.EXIT_THREAD_DEBUG_EVENT: break; case DebugEventTypes.CREATE_PROCESS_DEBUG_EVENT: _fileHandle = debugEvent.u.CreateProcessInfo.hFile; _processId = debugEvent.dwProcessId; _processStarted = true; break; //Exit the loop case DebugEventTypes.EXIT_PROCESS_DEBUG_EVENT: case DebugEventTypes.RIP_EVENT: default: _continue = false; break; } } if (_signalEnd && _processStarted) { Process process; if (TryGetAttachedProcess(out process) == true) { process.CloseMainWindow(); } } ContinueDebugEvent(debugEvent.dwProcessId, debugEvent.dwThreadId, DebugStates.DBG_CONTINUE); }while (_continue); } finally { End(); TaskHandler.RunTask(delegate(object input) { if (OnExiting != null) { OnExiting(this, EventArgs.Empty); } }); } }