啥也不用解释说明,直接拷贝源码即可用,搜了好久才找到的,激动万分!!
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 using System.Collections;
6 using System.Diagnostics;
7 using System.Runtime.InteropServices;
8 /// <summary>
9 /// Summary description for ProcessTreeKillable.
10 /// </summary>
11 public class ProcessUtility
12 {
13 public static void KillTree(int processToKillId)
14 {
15 // Kill each child process
16 foreach (int childProcessId in GetChildProcessIds(processToKillId))
17 {
18 using (Process child = Process.GetProcessById(childProcessId))
19 {
20 child.Kill();
21
22 }
23
24 }
25
26 // Then kill this process
27 using (Process thisProcess = Process.GetProcessById(processToKillId))
28 {
29 thisProcess.Kill();
30
31 }
32
33 }
34
35 public static int GetParentProcessId(int processId)
36 {
37 int ParentID = 0;
38 int hProcess = OpenProcess(eDesiredAccess.PROCESS_QUERY_INFORMATION,
39 false, processId);
40 if (hProcess != 0)
41 {
42 try
43 {
44 PROCESS_BASIC_INFORMATION pbi = new PROCESS_BASIC_INFORMATION();
45 int pSize = 0;
46 if (-1 != NtQueryInformationProcess(hProcess,
47 PROCESSINFOCLASS.ProcessBasicInformation, ref pbi, pbi.Size, ref
48 pSize))
49 {
50 ParentID = pbi.InheritedFromUniqueProcessId;
51
52 }
53
54 }
55
56 finally
57 {
58 CloseHandle(hProcess);
59
60 }
61
62 }
63
64 return (ParentID);
65
66 }
67
68 public static int[] GetChildProcessIds(int parentProcessId)
69 {
70 ArrayList myChildren = new ArrayList();
71 foreach (Process proc in Process.GetProcesses())
72 {
73 int currentProcessId = proc.Id;
74 proc.Dispose();
75 if (parentProcessId == GetParentProcessId(currentProcessId))
76 {
77 // Add this one
78 myChildren.Add(currentProcessId);
79 // Add any of its children
80 myChildren.AddRange(GetChildProcessIds(currentProcessId));
81
82 }
83
84 }
85
86 return (int[])myChildren.ToArray(typeof(int));
87
88 }
89
90 #region PInvokes
91 [DllImport("KERNEL32.DLL")]
92 private static extern int OpenProcess(eDesiredAccess dwDesiredAccess,
93 bool bInheritHandle, int dwProcessId);
94 [DllImport("KERNEL32.DLL")]
95 private static extern int CloseHandle(int hObject);
96 [DllImport("NTDLL.DLL")]
97 private static extern int NtQueryInformationProcess(int hProcess,
98 PROCESSINFOCLASS pic, ref PROCESS_BASIC_INFORMATION pbi, int cb, ref
99 int pSize);
100 private enum PROCESSINFOCLASS : int
101 {
102 ProcessBasicInformation = 0,
103 ProcessQuotaLimits,
104 ProcessIoCounters,
105 ProcessVmCounters,
106 ProcessTimes,
107 ProcessBasePriority,
108 ProcessRaisePriority,
109 ProcessDebugPort,
110 ProcessExceptionPort,
111 ProcessAccessToken,
112 ProcessLdtInformation,
113 ProcessLdtSize,
114 ProcessDefaultHardErrorMode,
115 ProcessIoPortHandlers,
116 // Note: this is kernel mode only
117 ProcessPooledUsageAndLimits,
118 ProcessWorkingSetWatch,
119 ProcessUserModeIOPL,
120 ProcessEnableAlignmentFaultFixup,
121 ProcessPriorityClass,
122 ProcessWx86Information,
123 ProcessHandleCount,
124 ProcessAffinityMask,
125 ProcessPriorityBoost,
126 MaxProcessInfoClass
127
128 };
129
130 [StructLayout(LayoutKind.Sequential)]
131 private struct PROCESS_BASIC_INFORMATION
132 {
133 public int ExitStatus;
134 public int PebBaseAddress;
135 public int AffinityMask;
136 public int BasePriority;
137 public int UniqueProcessId;
138 public int InheritedFromUniqueProcessId;
139 public int Size
140 {
141 get
142 {
143 return (6 * 4);
144 }
145
146 }
147
148 };
149
150 private enum eDesiredAccess : int
151 {
152 DELETE = 0x00010000,
153 READ_CONTROL = 0x00020000,
154 WRITE_DAC = 0x00040000,
155 WRITE_OWNER = 0x00080000,
156 SYNCHRONIZE = 0x00100000,
157 STANDARD_RIGHTS_ALL = 0x001F0000,
158 PROCESS_TERMINATE = 0x0001,
159 PROCESS_CREATE_THREAD = 0x0002,
160 PROCESS_SET_SESSIONID = 0x0004,
161 PROCESS_VM_OPERATION = 0x0008,
162 PROCESS_VM_READ = 0x0010,
163 PROCESS_VM_WRITE = 0x0020,
164 PROCESS_DUP_HANDLE = 0x0040,
165 PROCESS_CREATE_PROCESS = 0x0080,
166 PROCESS_SET_QUOTA = 0x0100,
167 PROCESS_SET_INFORMATION = 0x0200,
168 PROCESS_QUERY_INFORMATION = 0x0400,
169 PROCESS_ALL_ACCESS = SYNCHRONIZE | 0xFFF
170
171 }
172 #endregion
173
174 }
2 using System.Collections.Generic;
3 using System.Text;
4
5 using System.Collections;
6 using System.Diagnostics;
7 using System.Runtime.InteropServices;
8 /// <summary>
9 /// Summary description for ProcessTreeKillable.
10 /// </summary>
11 public class ProcessUtility
12 {
13 public static void KillTree(int processToKillId)
14 {
15 // Kill each child process
16 foreach (int childProcessId in GetChildProcessIds(processToKillId))
17 {
18 using (Process child = Process.GetProcessById(childProcessId))
19 {
20 child.Kill();
21
22 }
23
24 }
25
26 // Then kill this process
27 using (Process thisProcess = Process.GetProcessById(processToKillId))
28 {
29 thisProcess.Kill();
30
31 }
32
33 }
34
35 public static int GetParentProcessId(int processId)
36 {
37 int ParentID = 0;
38 int hProcess = OpenProcess(eDesiredAccess.PROCESS_QUERY_INFORMATION,
39 false, processId);
40 if (hProcess != 0)
41 {
42 try
43 {
44 PROCESS_BASIC_INFORMATION pbi = new PROCESS_BASIC_INFORMATION();
45 int pSize = 0;
46 if (-1 != NtQueryInformationProcess(hProcess,
47 PROCESSINFOCLASS.ProcessBasicInformation, ref pbi, pbi.Size, ref
48 pSize))
49 {
50 ParentID = pbi.InheritedFromUniqueProcessId;
51
52 }
53
54 }
55
56 finally
57 {
58 CloseHandle(hProcess);
59
60 }
61
62 }
63
64 return (ParentID);
65
66 }
67
68 public static int[] GetChildProcessIds(int parentProcessId)
69 {
70 ArrayList myChildren = new ArrayList();
71 foreach (Process proc in Process.GetProcesses())
72 {
73 int currentProcessId = proc.Id;
74 proc.Dispose();
75 if (parentProcessId == GetParentProcessId(currentProcessId))
76 {
77 // Add this one
78 myChildren.Add(currentProcessId);
79 // Add any of its children
80 myChildren.AddRange(GetChildProcessIds(currentProcessId));
81
82 }
83
84 }
85
86 return (int[])myChildren.ToArray(typeof(int));
87
88 }
89
90 #region PInvokes
91 [DllImport("KERNEL32.DLL")]
92 private static extern int OpenProcess(eDesiredAccess dwDesiredAccess,
93 bool bInheritHandle, int dwProcessId);
94 [DllImport("KERNEL32.DLL")]
95 private static extern int CloseHandle(int hObject);
96 [DllImport("NTDLL.DLL")]
97 private static extern int NtQueryInformationProcess(int hProcess,
98 PROCESSINFOCLASS pic, ref PROCESS_BASIC_INFORMATION pbi, int cb, ref
99 int pSize);
100 private enum PROCESSINFOCLASS : int
101 {
102 ProcessBasicInformation = 0,
103 ProcessQuotaLimits,
104 ProcessIoCounters,
105 ProcessVmCounters,
106 ProcessTimes,
107 ProcessBasePriority,
108 ProcessRaisePriority,
109 ProcessDebugPort,
110 ProcessExceptionPort,
111 ProcessAccessToken,
112 ProcessLdtInformation,
113 ProcessLdtSize,
114 ProcessDefaultHardErrorMode,
115 ProcessIoPortHandlers,
116 // Note: this is kernel mode only
117 ProcessPooledUsageAndLimits,
118 ProcessWorkingSetWatch,
119 ProcessUserModeIOPL,
120 ProcessEnableAlignmentFaultFixup,
121 ProcessPriorityClass,
122 ProcessWx86Information,
123 ProcessHandleCount,
124 ProcessAffinityMask,
125 ProcessPriorityBoost,
126 MaxProcessInfoClass
127
128 };
129
130 [StructLayout(LayoutKind.Sequential)]
131 private struct PROCESS_BASIC_INFORMATION
132 {
133 public int ExitStatus;
134 public int PebBaseAddress;
135 public int AffinityMask;
136 public int BasePriority;
137 public int UniqueProcessId;
138 public int InheritedFromUniqueProcessId;
139 public int Size
140 {
141 get
142 {
143 return (6 * 4);
144 }
145
146 }
147
148 };
149
150 private enum eDesiredAccess : int
151 {
152 DELETE = 0x00010000,
153 READ_CONTROL = 0x00020000,
154 WRITE_DAC = 0x00040000,
155 WRITE_OWNER = 0x00080000,
156 SYNCHRONIZE = 0x00100000,
157 STANDARD_RIGHTS_ALL = 0x001F0000,
158 PROCESS_TERMINATE = 0x0001,
159 PROCESS_CREATE_THREAD = 0x0002,
160 PROCESS_SET_SESSIONID = 0x0004,
161 PROCESS_VM_OPERATION = 0x0008,
162 PROCESS_VM_READ = 0x0010,
163 PROCESS_VM_WRITE = 0x0020,
164 PROCESS_DUP_HANDLE = 0x0040,
165 PROCESS_CREATE_PROCESS = 0x0080,
166 PROCESS_SET_QUOTA = 0x0100,
167 PROCESS_SET_INFORMATION = 0x0200,
168 PROCESS_QUERY_INFORMATION = 0x0400,
169 PROCESS_ALL_ACCESS = SYNCHRONIZE | 0xFFF
170
171 }
172 #endregion
173
174 }