#include <File.au3>
#include <File.au3>
#include <WinAPI.au3>
$tagProcessEntry = "dword dwSize;dword cntUsage;" & _
"dword th32ProcessID;ulong_ptr th32DefaultHeapID;" & _
"dword th32ModuleID;dword cntThreads;" & _
"dword th32ParentProcessID;long pcPriClassBase;" & _
"dword dwFlags;char szExeFile[260]"
Func _CreateToolhelp32Snapshot($iProcessId, $iFlag)
Local $hSnap
$iProcessId = ProcessExists($iProcessId)
$hSnap = DllCall("kernel32.dll", "hWnd", "CreateToolhelp32Snapshot", _
"dword", $iFlag, "dword", $iProcessId)
Return $hSnap[0]
EndFunc ;==>_CreateToolhelp32Snapshot()
Func _Process32First($hSnap)
Local $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 _EnumChildProcesses($iProcessId)
Local $hSnap, $tProcessEntry, $aResult[1], $iProcess, $iParent
$hSnap = _CreateToolhelp32Snapshot(0, 2)
$tProcessEntry = _Process32First($hSnap)
$iProcessId = ProcessExists($iProcessId)
If not $iProcessId Then Return SetError(@error, 0, 0)
While True
_Process32Next($hSnap, $tProcessEntry)
If @error Then ExitLoop
$iProcess = DllStructGetData($tProcessEntry, "th32ProcessID")
$iParent = DllStructGetData($tProcessEntry, "th32ParentProcessID")
If $iParent = $iProcessId Then
$aResult[Ubound($aResult) - 1] = $iProcess
Redim $aResult[Ubound($aResult) + 1]
EndIf
WEnd
$tProcessEntry = 0
_WinAPI_CloseHandle($hSnap)
If Ubound($aResult) = 1 Then Return SetError(2, 0, 0)
Redim $aResult[Ubound($aResult) - 1]
Return SetError(0, Ubound($aResult), $aResult)
EndFunc ;==>_EnumChildProcesses()
Func _QueryParentProcess($iChildProcessId, $lparam = 0)
Local $iResult, $tProcess, $pProcess, $hSnap
Local $sResult, $sDrive, $sPath, $sName, $sExt
$iChildProcessId = ProcessExists($iChildProcessId)
If Not $iChildProcessId Then Return SetError(2, 0, 0)
$hSnap = _CreateToolhelp32Snapshot(0, 2)
$tProcess = _Process32First($hSnap)
While True
_Process32Next($hSnap, $tProcess)
If @error Then ExitLoop
$iProcessId = DllStructGetData($tProcess, "th32ProcessId")
$iParentId = DllStructGetData($tProcess, "th32ParentProcessID")
If $iProcessId = $iChildProcessId And $lparam = 0 Then
_WinAPI_CloseHandle($hSnap)
$sResult = _QueryParentProcess($iParentId, 1)
If $sResult Then Return $sResult
$sResult = _GetModuleFileNameEx($iProcessId)
_PathSplit($sResult, $sDrive, $sPath, $sName, $sExt)
Return $sDrive & $sPath
ElseIf $lparam = 1 And $iProcessId = $iChildProcessId Then
_WinAPI_CloseHandle($hSnap)
$sResult = _GetModuleFileNameEx($iProcessId)
_PathSplit($sResult, $sDrive, $sPath, $sName, $sExt)
Return $sDrive & $sPath
EndIf
WEnd
EndFunc ;==>_QueryParentProcess()
Func _GetModuleFileNameEx($iProcessId)
Local $iResult, $hProcess
$iProcessId = ProcessExists($iProcessId)
If Not $iProcessId Then Return SetError(2, 0, "")
$hProcess = _WinAPI_OpenProcess(0x400+0x10, 0, $iProcessId)
$iResult = DllCall("psapi.dll", "int", "GetModuleFileNameEx", _
"hWnd", $hProcess, "hWnd", 0, "str", "", "int", 260)
_WinAPI_CloseHandle($hProcess)
Return SetError(Not $iResult[0], 0, $iResult[3])
EndFunc ;==>_GetModuleFileNameEx()
Msgbox(0, _QueryParentProcess("qq.exe"), @error)