小改一下代码,让返回数组类似于ProcessList函数返回值,同时包含进程名称与进程ID。
#Include <Array.au3>
Local $aChild = _EnumChildProcesses("explorer.exe")
_ArrayDisplay($aChild)
Exit
;===============================================================================
; 说明: 枚举子进程
; 语法: _EnumChildProcesses($iPID)
; 参数: $iPID - 进程名称或ID
; 需要: 无
; 返回: 成功 - 二维数组, 结构类似于 ProcessList 函数返回值
; 失败 - 空字符 "", 并设置 @error 到
; 1 - 进程不存在
; 2 - 子进程未找到
; 备注: 参考: http://www.autoitx.com/forum.php?mod=viewthread&tid=6284
;===============================================================================
Func _EnumChildProcesses($iPID)
Local $hSnap, $tProcessEntry, $iParent, $iProcess, $sExeFile, $aResult[1][2] = [[0]]
$iPID = ProcessExists($iPID)
If $iPID = 0 Then Return SetError(1, 0, "")
$hSnap = _CreateToolhelp32Snapshot(0, 2)
$tProcessEntry = _Process32First($hSnap)
While 1
_Process32Next($hSnap, $tProcessEntry)
If @error Then ExitLoop
$iParent = DllStructGetData($tProcessEntry, "th32ParentProcessID")
If $iParent = $iPID Then
$sExeFile = DllStructGetData($tProcessEntry, "szExeFile")
$iProcess = DllStructGetData($tProcessEntry, "th32ProcessID")
$aResult[0][0] += 1
ReDim $aResult[$aResult[0][0] + 1][2]
$aResult[$aResult[0][0]][0] = $sExeFile
$aResult[$aResult[0][0]][1] = $iProcess
EndIf
WEnd
_CloseHandle($hSnap)
If $aResult[0][0] = 0 Then
Return SetError(2, 0, "")
Else
Return $aResult
EndIf
EndFunc ;==>_EnumChildProcesses
Func _CreateToolhelp32Snapshot($iPID, $iFlag)
Local $hSnap
$hSnap = DllCall("kernel32.dll", "hWnd", "CreateToolhelp32Snapshot", "dword", $iFlag, "dword", $iPID)
Return $hSnap[0]
EndFunc ;==>_CreateToolhelp32Snapshot
Func _Process32First($hSnap)
Local $tagProcessEntry, $tProcessEntry
$tagProcessEntry = "dword dwSize;dword cntUsage;" & _
"dword th32ProcessID;ulong_ptr th32DefaultHeapID;" & _
"dword th32ModuleID;dword cntThreads;" & _
"dword th32ParentProcessID;long pcPriClassBase;" & _
"dword dwFlags;char szExeFile[260]"
$tProcessEntry = DllStructCreate($tagProcessEntry)
DllStructSetData($tProcessEntry, 1, DllStructGetSize($tProcessEntry))
DllCall("kernel32.dll", "int", "Process32First", "hWnd", $hSnap, "ptr", DllStructGetPtr($tProcessEntry))
Return $tProcessEntry
EndFunc ;==>_Process32First
Func _Process32Next(ByRef $hSnap, ByRef $tProcessEntry)
Local $iResult
$iResult = DllCall("kernel32.dll", "int", "Process32Next", "hWnd", $hSnap, "ptr", DllStructGetPtr($tProcessEntry))
Return SetError(NOT $iResult[0], 0, $iResult[0])
EndFunc ;==>_Process32Next
Func _CloseHandle($hWnd)
Local $iResult
$iResult = DllCall("kernel32.dll", "int", "CloseHandle", "hWnd", $hWnd)
Return $iResult[0] <> 0
EndFunc ;==>_CloseHandle
|