private static void TerminateProcessTree(int pid, ProcessRelationship[] relationchips) { if (pid == 0) { return; } //terminate childs recursively for (int i = relationchips.Length; i-- > 0;) { ProcessRelationship relationchip = relationchips[i]; if (relationchip.ParentPid == pid) { relationchip.ParentPid = 0; TerminateProcessTree(relationchip.Pid, relationchips); } } //terminate the process IntPtr procHandle = Native.OpenProcess(Native.PROCESS_ALL_ACCESS, false, pid); if (procHandle != IntPtr.Zero) { try { Native.TerminateProcess(procHandle, -1); } catch { } finally { Native.CloseHandle(procHandle); } } }
/// <summary> /// Returns all the process relationships. /// </summary> /// <returns></returns> public static unsafe ProcessRelationship[] GetProcessRelationships() { var relationchips = new List <ProcessRelationship>(100); IntPtr snapshot = Native.CreateToolhelp32Snapshot(Native.TH32CS_SNAPPROCESS, 0); int bufferSize = sizeof(Native.WinProcessEntry); IntPtr buffer = Marshal.AllocHGlobal(bufferSize); Marshal.WriteInt32(buffer, bufferSize); var natEntryPtr = (Native.WinProcessEntry *)buffer; try { if (!Native.Process32First(snapshot, buffer)) { throw new Win32Exception(); } do { Native.WinProcessEntry natEntry = *natEntryPtr; ProcessRelationship relationchip = new ProcessRelationship(); relationchip.Pid = natEntry.th32ProcessID; relationchip.ParentPid = natEntry.th32ParentProcessID; relationchips.Add(relationchip); } while (Native.Process32Next(snapshot, buffer)); } finally { Marshal.FreeHGlobal(buffer); Native.CloseHandle(snapshot); } return(relationchips.ToArray()); }
private static void TerminateProcessTree(int pid, ProcessRelationship[] relationchips) { if (pid == 0) return; //terminate childs recursively for (int i = relationchips.Length; i-- > 0; ) { ProcessRelationship relationchip = relationchips[i]; if (relationchip.ParentPid == pid) { relationchip.ParentPid = 0; TerminateProcessTree(relationchip.Pid, relationchips); } } //terminate the process IntPtr procHandle = Native.OpenProcess(Native.PROCESS_ALL_ACCESS, false, pid); if (procHandle != IntPtr.Zero) { try { Native.TerminateProcess(procHandle, -1); } catch { } finally { Native.CloseHandle(procHandle); } } }
/// <summary> /// Returns all the process relationships. /// </summary> /// <returns></returns> public static unsafe ProcessRelationship[] GetProcessRelationships() { var relationchips = new List<ProcessRelationship>(100); IntPtr snapshot = Native.CreateToolhelp32Snapshot(Native.TH32CS_SNAPPROCESS, 0); int bufferSize = sizeof(Native.WinProcessEntry); IntPtr buffer = Marshal.AllocHGlobal(bufferSize); Marshal.WriteInt32(buffer, bufferSize); var natEntryPtr = (Native.WinProcessEntry*)buffer; try { if (!Native.Process32First(snapshot, buffer)) throw new Win32Exception(); do { Native.WinProcessEntry natEntry = *natEntryPtr; ProcessRelationship relationchip = new ProcessRelationship(); relationchip.Pid = natEntry.th32ProcessID; relationchip.ParentPid = natEntry.th32ParentProcessID; relationchips.Add(relationchip); } while (Native.Process32Next(snapshot, buffer)); } finally { Marshal.FreeHGlobal(buffer); Native.CloseHandle(snapshot); } return relationchips.ToArray(); }