Pfad von Prozess auslesen?

  • Hi ich bins mal wieder und habe eine neue Frage wozu ich nichts finde :(

    Kann man / wie kann man sich den Pfad anzeigen lassen, den ein laufender prozess hat?
    Also ich habe jetzt zum Beispiel firefox.exe in den prozessen und will wissen, wo die firefox.exe denn nun liegt also in welchem Ordner.
    Ich könnte das natürlich manuell machen und firefox.exe bei "suchen" eingeben, aber das script soll es natürlich schaffen :)
    Bin über jeden Tipp dankbar.

    LG qon

    Einmal editiert, zuletzt von qon (9. Dezember 2007 um 16:04)

  • [autoit]

    $Pfad = _getProcessPath("firefox.exe")

    [/autoit][autoit][/autoit][autoit]

    If Not @error Then MsgBox(0, "", "Prozesspfad: " & $Pfad)

    [/autoit][autoit][/autoit][autoit]

    Func _getProcessPath($ProcessName)
    Local $Item
    Local $Object = ObjGet("winmgmts:\\localhost\root\CIMV2")
    If @error Then Return SetError(2, 2, '')

    [/autoit][autoit][/autoit][autoit]

    For $Item In $Object.ExecQuery ("SELECT ExecutablePath FROM Win32_Process WHERE Name='" & $ProcessName & "'", "WQL", 48)
    Return $Item.ExecutablePath
    Next
    Return SetError(1, 1, '')
    EndFunc

    [/autoit]
  • Will meine Lösung auch noch posten:

    Spoiler anzeigen
    [autoit]

    #include <array.au3>
    $names = ProcessList("firefox.exe")
    ReDim $names[UBound($names)][3]
    For $i = 1 To UBound($names)-1
    $names[$i][2] = _ProcessGetPath($names[$i][1])
    ;MsgBox(0, '', _ProcessGetPath($names[$i][1]))
    Next
    _ArrayDisplay($names)
    Exit
    ;===============================================================================
    ;
    ; Function Name: _ProcessGetPath()
    ; Description:: gets the path of a process by it's PID
    ; Parameter(s): $PID - Process ID
    ; Requirement(s): DLL-Functions
    ; Return Value(s): Path of Process
    ; Author(s): Prog@ndy
    ;
    ;===============================================================================
    ;
    Func _ProcessGetPath($PID)
    $Path = DllStructCreate("char[1000]")
    $dll = DllOpen("Kernel32.dll")
    $handle = DllCall($dll,"int", "OpenProcess","dword",0x0400 + 0x0010,"int",0,"dword",$PID)
    $ret = DllCall("Psapi.dll","long","GetModuleFileNameEx","long",$handle[0],"int",0,"ptr",DllStructGetPtr($Path),"long",DllStructGetSize($Path))
    $ret = DllCall($dll,"int", "CloseHandle","hwnd",$handle[0])
    DllClose($dll)
    $ProgramPath = DllStructGetData($Path,1)
    Return $ProgramPath
    EndFunc

    [/autoit]
  • Schöne Lösung - gefällt mir.
    Dürfte um ein vielfaches performanter sein als eine WMI-basierte Lösung.
    Hab die noch minimal angepasst damit sie auch mit Prozessnamen statt nur mit ProzessID´s klarkommt.
    Somit kann man sich den Teil mit ProcessList sparen:

    Spoiler anzeigen
    [autoit]

    $Pfad = _ProcessGetPath("firefox.exe")

    [/autoit] [autoit][/autoit] [autoit]

    MsgBox(0, "", "Prozesspfad: " & $Pfad)

    [/autoit] [autoit][/autoit] [autoit]

    Func _ProcessGetPath($PID)
    If IsString($PID) Then $PID = ProcessExists($PID)
    $Path = DllStructCreate("char[1000]")
    $dll = DllOpen("Kernel32.dll")
    $handle = DllCall($dll, "int", "OpenProcess", "dword", 0x0400 + 0x0010, "int", 0, "dword", $PID)
    $ret = DllCall("Psapi.dll", "long", "GetModuleFileNameEx", "long", $handle[0], "int", 0, "ptr", DllStructGetPtr($Path), "long", DllStructGetSize($Path))
    $ret = DllCall($dll, "int", "CloseHandle", "hwnd", $handle[0])
    DllClose($dll)
    Return DllStructGetData($Path, 1)
    EndFunc

    [/autoit]

    Natürlich nur sinnvoll wenn es nur einen Prozess mit diesem Namen gibt.

    2 Mal editiert, zuletzt von AspirinJunkie (9. Dezember 2007 um 18:06)