Articles
FAQs
Login
Killing all child and parent processes of specified process
By Perry
ODBC Drivers for QuickBooks, Salesforce, SAP, MSCRM, SharePoint … Free Trial!
You can use this code to kill all child and parent processes of specified process. The program uses the unmanaged code to kill the upward and downward process tree.
Complete VB.NET code
--------------------------
Function GetParentProcessID(ByVal id As Integer) As Integer
Private Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.DLL" (ByVal dwFlags As Integer, ByVal th32ProcessID As Integer) As Integer
Private Declare Function Process32First Lib "KERNEL32.DLL" (ByVal hSnapshot As Integer, ByVal PE As Byte()) As Integer
Private Declare Function Process32Next Lib "KERNEL32.DLL" (ByVal hSnapshot As Integer, ByVal PE As Byte()) As Integer
Private Declare Function CloseHandle Lib "KERNEL32.DLL" (ByVal hObject As Integer) As Integer
Dim b(564 - 1) As Byte
BitConverter.GetBytes(SIZEOF_PROCESSENTRY32).CopyT(o(b, SIZE_OFFSET))
Dim h As Integer = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
Try
Dim rv As Integer = Process32First(h, b)
If rv <> 1 Then
Throw New Exception("Could not enumerte processes.")
End If
While rv = 1
Dim pid As Integer = BitConverter.ToInt32(b, PROCESS_OFFSET)
Dim parent As Integer = BitConverter.ToInt32(b, PARENT_OFFSET)
If pid = id Then
Return parent
End If
rv = Process32Next(h, b)
End While
Finally
CloseHandle(h)
End Try
Return -1
End Function
Private Sub KillAllAssociatedProcesses(ByVal track As String)
Dim localAll As Process() = Process.GetProcesses()
For Each i As Process In localAll
Dim ParentProcessID As Integer
ParentProcessID = GetParentProcessID(i.Id)
If ParentProcessID = ClientTracks(Track).Process.Id Then
Process.GetProcessById(i.Id).Kill()
End If
Next
End Sub
Module Module1
Sub Main()
Call KillAllAssociatedProcesses("processName")
End Sub
End Module
Regards,
Megha
Popularity
(
1395 Views
)