Juno_okyo

_ProcessGetPath (Retrieves a process file path)

Oct 9th, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.96 KB | None | 0 0
  1. ; #FUNCTION# ===============================================================================
  2. ; Name...........: _ProcessGetPath(
  3. ; Description ...: Retrieves a process file path
  4. ; Syntax.........: _ProcessGetPath($vProcess)
  5. ; Parameters ....: $vProcess - PID or name of a process
  6. ; Requirements...: kernel32.dll, psapi.dll
  7. ; Return values .: Success - A full process path
  8. ;                    @error = 0
  9. ;                   Failure - Empty string
  10. ;                    @error = 1 - Invalid process name/PID
  11. ;                    @error = 2 - kernel32.dll failed to open (wrong version?)
  12. ;                    @error = 3 - Could not OpenProcess
  13. ;                     @error = 4 - psapi.dll failed to open (doesn't exist?)
  14. ;                    @error = 5 - returned path is empty or invalid
  15. ; Author ........: JScript, Larry, SmOke_N
  16. ; Modified.......: mrRevoked - reformated, error checking
  17. ; Remarks .......:
  18. ; Related .......:
  19. ; Link ..........;
  20. ; Example .......;
  21. ; ============================================================================================
  22. Func _ProcessGetPath($vProcess)
  23.     Local $i_PID, $hKernel32, $hPsapi, $aProcessHandle, $tDLLStruct, $iError, $sProcessPath
  24.  
  25.     $i_PID = ProcessExists($vProcess)
  26.  
  27.     If Not $i_PID Then Return SetError(1, 0, "");process doesn't exist?
  28.  
  29.     $hKernel32 = DllOpen("Kernel32.dll")
  30.     $iError = @error
  31.     If $iError Then
  32.         DllClose($hKernel32)
  33.         Return SetError(2, $iError, ""); dllopen kernell32.dll failed
  34.     EndIf
  35.  
  36.     $aProcessHandle = DllCall($hKernel32, "int", "OpenProcess", "int", 0x0400 + 0x0010, "int", 0, "int", $i_PID)
  37.     $iError = @error
  38.     If $iError Or $aProcessHandle[0] = 0 Then
  39.         DllClose($hKernel32)
  40.         Return SetError(2, $iError, "");openprocess failed
  41.     EndIf
  42.  
  43.     $hPsapi = DllOpen("Psapi.dll")
  44.     $iError = @error
  45.     If $iError Then
  46.         DllClose($hKernel32)
  47.         DllClose($hPsapi)
  48.         Return SetError(3, $iError, ""); dllopen psapi.dll failed
  49.     EndIf
  50.  
  51.     $tDLLStruct = DllStructCreate("char[1000]")
  52.  
  53.     DllCall($hPsapi, "long", "GetModuleFileNameEx", "int", $aProcessHandle[0], "int", 0, "ptr", DllStructGetPtr($tDLLStruct), "long", DllStructGetSize($tDLLStruct))
  54.     $iError = @error
  55.  
  56.     DllCall($hKernel32, "int", "CloseHandle", "int", $aProcessHandle[0])
  57.     DllClose($hKernel32)
  58.     DllClose($hPsapi)
  59.  
  60.     If $iError Then
  61.         $tDLLStruct = 0
  62.         Return SetError(4, $iError, "");getmodulefilenamex failed
  63.     EndIf
  64.  
  65.     $sProcessPath = DllStructGetData($tDLLStruct, 1)
  66.     $tDLLStruct = 0
  67.  
  68. ;format the output
  69.     If StringLen($sProcessPath) < 2 Then Return SetError(5, 0, "");is empty or non readable
  70.     If StringLeft($sProcessPath, 4) = "\??\" Then $sProcessPath = StringReplace($sProcessPath, "\??\", "")
  71.     If StringLeft($sProcessPath, 20) = "\SystemRoot\System32" Then $sProcessPath = StringReplace($sProcessPath, "\SystemRoot\System32", @SystemDir)
  72.  
  73.     Return SetError(0, 0, $sProcessPath)
  74. EndFunc;==>_ProcessGetPath
Add Comment
Please, Sign In to add comment