On Oct 3, 3:29 pm, SVog...@gmail.c om 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
GetParentProces sID. 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 CreateToolhelp3 2Snapshot Lib
"KERNEL32.D LL" _
(ByVal dwFlags As Integer, ByVal th32ProcessID As Integer) As
Integer
Private Declare Function Process32First Lib "KERNEL32.D LL" _
(ByVal hSnapshot As Integer, ByVal PE As Byte()) As Integer
Private Declare Function Process32Next Lib "KERNEL32.D LL" _
(ByVal hSnapshot As Integer, ByVal PE As Byte()) As Integer
Private Declare Function CloseHandle Lib "KERNEL32.D LL" _
(ByVal hObject As Integer) As Integer
Private Sub KillAllAssociat edProcesses(ByV al track As String)
Dim localAll As Process() = Process.GetProc esses()
For Each i As Process In localAll
Dim ParentProcessID As Integer
ParentProcessID = GetParentProces sID(i.Id)
If ParentProcessID = ClientTracks(Tr ack).Process.Id Then
Process.GetProc essById(i.Id).K ill()
End If
Next
End Sub
Function GetParentProces sID(ByVal id As Integer) As Integer
Dim b(564 - 1) As Byte
'write the size into the structure
BitConverter.Ge tBytes(SIZEOF_P ROCESSENTRY32). CopyTo(b,
SIZE_OFFSET)
Dim h As Integer =
CreateToolhelp3 2Snapshot(TH32C S_SNAPPROCESS, 0)
Try
Dim rv As Integer = Process32First( h, b)
If rv <1 Then
Throw New Exception("Coul d not enumerte processes.")
End If
While rv = 1
Dim pid As Integer = BitConverter.To Int32(b,
PROCESS_OFFSET)
Dim parent As Integer = BitConverter.To Int32(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