OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 namespace NativeClientVSAddIn | 5 namespace NativeClientVSAddIn |
6 { | 6 { |
7 using System; | 7 using System; |
8 using System.Collections.Generic; | 8 using System.Collections.Generic; |
9 using System.Linq; | 9 using System.Linq; |
10 using System.Management; | 10 using System.Management; |
(...skipping 30 matching lines...) Expand all Loading... |
41 /// Searches the system for all processes of a given process ID. | 41 /// Searches the system for all processes of a given process ID. |
42 /// </summary> | 42 /// </summary> |
43 /// <param name="procID">ID to search for.</param> | 43 /// <param name="procID">ID to search for.</param> |
44 /// <returns>List of matching processes.</returns> | 44 /// <returns>List of matching processes.</returns> |
45 public List<ProcessInfo> GetResultsByID(uint procID) | 45 public List<ProcessInfo> GetResultsByID(uint procID) |
46 { | 46 { |
47 return GetResults(p => procID == p.ID); | 47 return GetResults(p => procID == p.ID); |
48 } | 48 } |
49 | 49 |
50 /// <summary> | 50 /// <summary> |
| 51 /// Returns a list of all descendant processes of a process. |
| 52 /// </summary> |
| 53 /// <param name="root">Process ID of the process to get all descendants from
.</param> |
| 54 /// <returns>All descendants of the given root process.</returns> |
| 55 public List<ProcessInfo> GetDescendants(uint root) |
| 56 { |
| 57 List<ProcessInfo> processes = GetSystemProcesses(); |
| 58 HashSet<ProcessInfo> descendants = new HashSet<ProcessInfo>(); |
| 59 descendants.UnionWith(processes.Where(p => p.ID == root)); |
| 60 int lastCount = 0; |
| 61 while (descendants.Count > lastCount) |
| 62 { |
| 63 lastCount = descendants.Count; |
| 64 |
| 65 // Add any processes who have a parent that is in the descendant list al
ready. |
| 66 // Note we check the creation date to prevent cycles caused by recycled
process IDs. |
| 67 descendants.UnionWith(processes.Where(p => descendants.Any( |
| 68 parent => parent.ID == p.ParentID && parent.CreationDate <= p.Creati
onDate))); |
| 69 } |
| 70 |
| 71 return descendants.ToList(); |
| 72 } |
| 73 |
| 74 /// <summary> |
51 /// Queries the system for the full list of processes. | 75 /// Queries the system for the full list of processes. |
52 /// </summary> | 76 /// </summary> |
53 /// <returns>List of processes on the system.</returns> | 77 /// <returns>List of processes on the system.</returns> |
54 protected virtual List<ProcessInfo> GetSystemProcesses() | 78 protected virtual List<ProcessInfo> GetSystemProcesses() |
55 { | 79 { |
56 var processList = new List<ProcessInfo>(); | 80 var processList = new List<ProcessInfo>(); |
57 string query = "select * from Win32_Process"; | 81 string query = "select * from Win32_Process"; |
58 using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(qu
ery)) | 82 using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(qu
ery)) |
59 { | 83 { |
60 using (ManagementObjectCollection results = searcher.Get()) | 84 using (ManagementObjectCollection results = searcher.Get()) |
61 { | 85 { |
62 foreach (ManagementObject process in results) | 86 foreach (ManagementObject process in results) |
63 { | 87 { |
64 processList.Add((ProcessInfo)process); | 88 processList.Add((ProcessInfo)process); |
65 } | 89 } |
66 } | 90 } |
67 } | 91 } |
68 | 92 |
69 return processList; | 93 return processList; |
70 } | 94 } |
71 } | 95 } |
72 } | 96 } |
OLD | NEW |