How To End Process Tree

S

SVogt83

Is there a way in VB.NET to mimic the "End Process Tree" option found
in the task manager? I have searched the process class and can not
find any way to determine a relationship between any two processes.
Is it possible to stop a process and all its "children" in VB.NET?
 
S

SVogt83

Is there a way in VB.NET to mimic the "EndProcessTree" option found
in the task manager? I have searched theprocessclass and can not
find any way to determine a relationship between any two processes.
Is it possible to stop aprocessand all its "children" in VB.NET?

Incase anyone else has his problem, i have finaly found code to solve
it for my purposes. If you have a process already handled in your
program, and have its ID, then you can loop though all processes using
the process class and send the ID ov each process to
GetParentProcessID. If the ID of your process matches the returned ID
then you can kill it. If anyone happens to read this and spots a
problem please let me know. Thanks


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

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

Function GetParentProcessID(ByVal id As Integer) As Integer
Dim b(564 - 1) As Byte
'write the size into the structure
BitConverter.GetBytes(SIZEOF_PROCESSENTRY32).CopyTo(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top