Enumerate all children of a process

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Enumerate all children of a process

Post by Mistrel »

This example enumerates all children of a process given a process ID and will return a list of child process IDs. This information can be useful where it is necessary to terminate a process and its children when its children are not known.

Code: Select all

Procedure GetProcessChildIDs(ParentID, List __out_ChildIDs())
  Protected Entry.PROCESSENTRY32
  Protected hSnapshot
  
  NewList ChildIDs()
 
  Entry\dwSize=SizeOf(PROCESSENTRY32)
 
  hSnapshot=CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS,0)
 
  If hSnapshot=#INVALID_HANDLE_VALUE
    ProcedureReturn #False
  EndIf
 
  If Not Process32First_(hSnapshot,@Entry)
    ProcedureReturn #False
  EndIf
 
  Repeat
    If Entry\th32ParentProcessID=ParentID
      AddElement(__out_ChildIDs())
      __out_ChildIDs()=Entry\th32ProcessID
           
      ;/ Don't enumerate the idle process (id 0)
      If Entry\th32ProcessID
        GetProcessChildIDs(Entry\th32ProcessID,ChildIDs())
      EndIf
     
      ForEach ChildIDs()
        AddElement(__out_ChildIDs())
        __out_ChildIDs()=ChildIDs()
      Next
    EndIf
  Until Not Process32Next_(hSnapshot,@Entry)
 
  CloseHandle_(hSnapshot)
 
  ProcedureReturn #True
EndProcedure
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Enumerate all children of a process

Post by Kwai chang caine »

Very usefull, there are a few time i search the children process of IE
Thanks a lot for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply