Page 1 of 1

Enumerate all children of a process

Posted: Sun Jan 22, 2012 1:03 pm
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

Re: Enumerate all children of a process

Posted: Sun Jan 22, 2012 11:08 pm
by Kwai chang caine
Very usefull, there are a few time i search the children process of IE
Thanks a lot for sharing 8)