Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; #FUNCTION# ===============================================================================
- ; Name...........: _ProcessGetPath(
- ; Description ...: Retrieves a process file path
- ; Syntax.........: _ProcessGetPath($vProcess)
- ; Parameters ....: $vProcess - PID or name of a process
- ; Requirements...: kernel32.dll, psapi.dll
- ; Return values .: Success - A full process path
- ; @error = 0
- ; Failure - Empty string
- ; @error = 1 - Invalid process name/PID
- ; @error = 2 - kernel32.dll failed to open (wrong version?)
- ; @error = 3 - Could not OpenProcess
- ; @error = 4 - psapi.dll failed to open (doesn't exist?)
- ; @error = 5 - returned path is empty or invalid
- ; Author ........: JScript, Larry, SmOke_N
- ; Modified.......: mrRevoked - reformated, error checking
- ; Remarks .......:
- ; Related .......:
- ; Link ..........;
- ; Example .......;
- ; ============================================================================================
- Func _ProcessGetPath($vProcess)
- Local $i_PID, $hKernel32, $hPsapi, $aProcessHandle, $tDLLStruct, $iError, $sProcessPath
- $i_PID = ProcessExists($vProcess)
- If Not $i_PID Then Return SetError(1, 0, "");process doesn't exist?
- $hKernel32 = DllOpen("Kernel32.dll")
- $iError = @error
- If $iError Then
- DllClose($hKernel32)
- Return SetError(2, $iError, ""); dllopen kernell32.dll failed
- EndIf
- $aProcessHandle = DllCall($hKernel32, "int", "OpenProcess", "int", 0x0400 + 0x0010, "int", 0, "int", $i_PID)
- $iError = @error
- If $iError Or $aProcessHandle[0] = 0 Then
- DllClose($hKernel32)
- Return SetError(2, $iError, "");openprocess failed
- EndIf
- $hPsapi = DllOpen("Psapi.dll")
- $iError = @error
- If $iError Then
- DllClose($hKernel32)
- DllClose($hPsapi)
- Return SetError(3, $iError, ""); dllopen psapi.dll failed
- EndIf
- $tDLLStruct = DllStructCreate("char[1000]")
- DllCall($hPsapi, "long", "GetModuleFileNameEx", "int", $aProcessHandle[0], "int", 0, "ptr", DllStructGetPtr($tDLLStruct), "long", DllStructGetSize($tDLLStruct))
- $iError = @error
- DllCall($hKernel32, "int", "CloseHandle", "int", $aProcessHandle[0])
- DllClose($hKernel32)
- DllClose($hPsapi)
- If $iError Then
- $tDLLStruct = 0
- Return SetError(4, $iError, "");getmodulefilenamex failed
- EndIf
- $sProcessPath = DllStructGetData($tDLLStruct, 1)
- $tDLLStruct = 0
- ;format the output
- If StringLen($sProcessPath) < 2 Then Return SetError(5, 0, "");is empty or non readable
- If StringLeft($sProcessPath, 4) = "\??\" Then $sProcessPath = StringReplace($sProcessPath, "\??\", "")
- If StringLeft($sProcessPath, 20) = "\SystemRoot\System32" Then $sProcessPath = StringReplace($sProcessPath, "\SystemRoot\System32", @SystemDir)
- Return SetError(0, 0, $sProcessPath)
- EndFunc;==>_ProcessGetPath
Add Comment
Please, Sign In to add comment