; File: _ProcessGetChildren.au3 ; Source: http://www.autoitscript.com/forum/index.php?showtopic=78445 #include-once #include "..\StdLib\array.au3" ; ================================================================================================== ; Name: _ProcessGetChildren ; Purpose: Get a list of top-level child processes ; Syntax: ; _ProcessGetChildren($i_ParentPID) ; Returns: ; Success: ; If child processes ARE found ... ; Returns a two-dimensional array wherein ... ; $arr[n][0] = the PID of a child process ; $arr[n][1] = the name of that child process ; ; If child processes are NOT found ... ; Returns 0 ; ; Failure: ; The passed $i_ParentPID is returned unchanged; ; @error is set as follows ... ; 1 - the first call to a function within "Kernel32.dll" failed. ; 2 - the first call to a function within "Kernel32.dll" failed. ; ; Notes: ; If successful and no array is returned, then the passed process does not have child ; processes. ; ; #Region Test_harness ; #include ; Global $a_children = _ProcessGetChildren(252) ; _ArrayDisplay($a_children) ; #EndRegion ; Author: ; SmokeN - August 16, 2008; Kudos to UEZ ; ================================================================================================== Func _ProcessGetChildren($i_pid) ; First level children processes only Local Const $TH32CS_SNAPPROCESS = 0x00000002 Local $a_tool_help = DllCall("Kernel32.dll", "long", "CreateToolhelp32Snapshot", "int", $TH32CS_SNAPPROCESS, "int", 0) If IsArray($a_tool_help) = 0 Or $a_tool_help[0] = -1 Then Return SetError(1, 0, $i_pid) Local $tagPROCESSENTRY32 = _ DllStructCreate _ ( _ "dword dwsize;" & _ "dword cntUsage;" & _ "dword th32ProcessID;" & _ "uint th32DefaultHeapID;" & _ "dword th32ModuleID;" & _ "dword cntThreads;" & _ "dword th32ParentProcessID;" & _ "long pcPriClassBase;" & _ "dword dwFlags;" & _ "char szExeFile[260]" _ ) DllStructSetData($tagPROCESSENTRY32, 1, DllStructGetSize($tagPROCESSENTRY32)) Local $p_PROCESSENTRY32 = DllStructGetPtr($tagPROCESSENTRY32) Local $a_pfirst = DllCall("Kernel32.dll", "int", "Process32First", "long", $a_tool_help[0], "ptr", $p_PROCESSENTRY32) If IsArray($a_pfirst) = 0 Then Return SetError(2, 0, $i_pid) Local $a_pnext, $a_children[11][2] = [[10]], $i_child_pid, $i_parent_pid, $i_add = 0 $i_child_pid = DllStructGetData($tagPROCESSENTRY32, "th32ProcessID") If $i_child_pid <> $i_pid Then $i_parent_pid = DllStructGetData($tagPROCESSENTRY32, "th32ParentProcessID") If $i_parent_pid = $i_pid Then $i_add += 1 $a_children[$i_add][0] = $i_child_pid $a_children[$i_add][1] = DllStructGetData($tagPROCESSENTRY32, "szExeFile") EndIf EndIf While 1 $a_pnext = DLLCall("Kernel32.dll", "int", "Process32Next", "long", $a_tool_help[0], "ptr", $p_PROCESSENTRY32) If IsArray($a_pnext) And $a_pnext[0] = 0 Then ExitLoop $i_child_pid = DllStructGetData($tagPROCESSENTRY32, "th32ProcessID") If $i_child_pid <> $i_pid Then $i_parent_pid = DllStructGetData($tagPROCESSENTRY32, "th32ParentProcessID") If $i_parent_pid = $i_pid Then If $i_add = $a_children[0][0] Then ReDim $a_children[$a_children[0][0] + 11][2] $a_children[0][0] = $a_children[0][0] + 10 EndIf $i_add += 1 $a_children[$i_add][0] = $i_child_pid $a_children[$i_add][1] = DllStructGetData($tagPROCESSENTRY32, "szExeFile") EndIf EndIf WEnd If $i_add <> 0 Then ReDim $a_children[$i_add + 1][2] $a_children[0][0] = $i_add EndIf DllCall("Kernel32.dll", "int", "CloseHandle", "long", $a_tool_help[0]) If $i_add Then Return $a_children Return SetError(3, 0, 0) EndFunc