void RefreshProcesses() { this.listBoxProcesses.Items.Clear(); CorPublish cp = null; int curPid = System.Diagnostics.Process.GetCurrentProcess().Id; try { int count = 0; cp = new CorPublish(); { foreach (CorPublishProcess cpp in cp.EnumProcesses()) { if (curPid != cpp.ProcessId) // let's hide our process { string version = CorDebugger.GetDebuggerVersionFromPid(cpp.ProcessId); string s = "[" + cpp.ProcessId + "] [ver=" + version + "] " + cpp.DisplayName; this.listBoxProcesses.Items.Add(new Item(cpp.ProcessId, s)); count++; } } } // using if (count == 0) { this.listBoxProcesses.Items.Add(new Item(0, "(No active processes)")); } } catch(Exception) { if (cp == null) { this.listBoxProcesses.Items.Add(new Item(0, "(Can't enumerate processes")); } } }
public static void ActiveProcess(string arguments) { ArgParser ap = new ArgParser(arguments); if (ap.Count > 1) { throw new MDbgShellException("Wrong # of arguments."); } if (ap.Exists(0)) { int logicalPID = ap.AsInt(0); bool found = false; foreach (MDbgProcess ps in Debugger.Processes) if (ps.Number == logicalPID) { Debugger.Processes.Active = ps; found = true; break; } if (found) { Shell.DisplayCurrentLocation(); } else { throw new MDbgShellException("Invalid process number"); } } else { MDbgProcess ActiveProcess = Debugger.Processes.HaveActive ? Debugger.Processes.Active : null; WriteOutput("Active Process:"); bool haveProcesses = false; CorPublish corPublish = null; foreach (MDbgProcess p in Debugger.Processes) { haveProcesses = true; string processName = p.Name; string launchMode; if (processName == null) { // in case we're attached (as opposed to launching), // we don't know process name. // Let's find it through CorPublishApi try { if (corPublish == null) { corPublish = new CorPublish(); } processName = corPublish.GetProcess(p.CorProcess.Id).DisplayName; } catch { processName = "N/A"; } launchMode = "attached"; } else { launchMode = "launched"; } WriteOutput((ActiveProcess == p ? "*" : " ") + string.Format(CultureInfo.InvariantCulture, "{0}. [PID: {1}, {2}] {3}", p.Number, p.CorProcess.Id, launchMode, processName)); } if (!haveProcesses) { WriteOutput("No Active Process!"); } } }
/// <summary> /// Attempts to retrieve the path to the binary from a running process /// Returns null on failure /// </summary> /// <param name="processId">The process to get the binary for</param> /// <returns>The path to the primary executable or null if it could not be determined</returns> private static string GetBinaryPathFromPid(int processId) { string programBinary = null; try { CorPublish cp = new CorPublish(); CorPublishProcess cpp = cp.GetProcess(processId); programBinary = cpp.DisplayName; } catch { // try an alternate method using (ProcessSafeHandle ph = NativeMethods.OpenProcess( (int)(NativeMethods.ProcessAccessOptions.ProcessVMRead | NativeMethods.ProcessAccessOptions.ProcessQueryInformation | NativeMethods.ProcessAccessOptions.ProcessDupHandle | NativeMethods.ProcessAccessOptions.Synchronize), false, // inherit handle processId)) { if (!ph.IsInvalid) { StringBuilder sb = new StringBuilder(NativeMethods.MAX_PATH); int neededSize = sb.Capacity; NativeMethods.QueryFullProcessImageName(ph, 0, sb, ref neededSize); programBinary = sb.ToString(); } } } return programBinary; }