By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,713 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,713 IT Pros & Developers. It's quick & easy.

How To End Process Tree

P: n/a
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?

Oct 3 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Oct 3, 3:29 pm, SVog...@gmail.com wrote:
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).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

Oct 10 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.