Befehlszeilen der laufenden Prozesse auslesen

  • Mit der Funktion ProcessList erhält man die Prozessnamen und -PIDs der laufenden Prozesse. Wie kann ich aber deren Befehlszeile - wie sie im Windows-Taskmamager angezeigt wird - ermitteln.

    Vielen Dank schon mal im Voraus!!!

    Einmal editiert, zuletzt von DOheim (20. Juni 2012 um 08:02)

  • Spoiler anzeigen
    [autoit]

    $aProc = ProcessList()
    For $i = 1 To $aProc[0][0]
    ConsoleWrite($aProc[$i][0] & ':' & @TAB & _WinAPI_GetCommandLineFromPID($aProc[$i][1]) & @CRLF)
    Next

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

    ; by trancexx (http://www.autoitscript.com/forum/index.ph…ndpost&p=780861)
    Func _WinAPI_GetCommandLineFromPID($iPID)
    Local Static $h_Kernel32DLL = DllOpen("kernel32.dll")
    Local Static $h_ntdll = DllOpen("ntdll.dll")

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

    Local $aCall = DllCall($h_Kernel32DLL, "handle", "OpenProcess", _
    "dword", 1040, _ ; PROCESS_VM_READ | PROCESS_QUERY_INFORMATION
    "bool", 0, _
    "dword", $iPID)

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

    If @error Or Not $aCall[0] Then
    Return SetError(1, 0, "")
    EndIf
    Local $hProcess = $aCall[0]
    Local $tPROCESS_BASIC_INFORMATION = DllStructCreate("dword_ptr ExitStatus;" & _
    "ptr PebBaseAddress;" & _
    "dword_ptr AffinityMask;" & _
    "dword_ptr BasePriority;" & _
    "dword_ptr UniqueProcessId;" & _
    "dword_ptr InheritedFromUniqueProcessId")
    $aCall = DllCall($h_ntdll, "int", "NtQueryInformationProcess", _
    "handle", $hProcess, _
    "dword", 0, _ ; ProcessBasicInformation
    "ptr", DllStructGetPtr($tPROCESS_BASIC_INFORMATION), _
    "dword", DllStructGetSize($tPROCESS_BASIC_INFORMATION), _
    "dword*", 0)
    If @error Then
    DllCall($h_Kernel32DLL, "bool", "CloseHandle", "handle", $hProcess)
    Return SetError(2, 0, "")
    EndIf

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

    Local $tPEB = DllStructCreate("byte InheritedAddressSpace;" & _
    "byte ReadImageFileExecOptions;" & _
    "byte BeingDebugged;" & _
    "byte Spare;" & _
    "ptr Mutant;" & _
    "ptr ImageBaseAddress;" & _
    "ptr LoaderData;" & _
    "ptr ProcessParameters;" & _
    "ptr SubSystemData;" & _
    "ptr ProcessHeap;" & _
    "ptr FastPebLock;" & _
    "ptr FastPebLockRoutine;" & _
    "ptr FastPebUnlockRoutine;" & _
    "dword EnvironmentUpdateCount;" & _
    "ptr KernelCallbackTable;" & _
    "ptr EventLogSection;" & _
    "ptr EventLog;" & _
    "ptr FreeList;" & _
    "dword TlsExpansionCounter;" & _
    "ptr TlsBitmap;" & _
    "dword TlsBitmapBits[2];" & _
    "ptr ReadOnlySharedMemoryBase;" & _
    "ptr ReadOnlySharedMemoryHeap;" & _
    "ptr ReadOnlyStaticServerData;" & _
    "ptr AnsiCodePageData;" & _
    "ptr OemCodePageData;" & _
    "ptr UnicodeCaseTableData;" & _
    "dword NumberOfProcessors;" & _
    "dword NtGlobalFlag;" & _
    "ubyte Spare2[4];" & _
    "int64 CriticalSectionTimeout;" & _
    "dword HeapSegmentReserve;" & _
    "dword HeapSegmentCommit;" & _
    "dword HeapDeCommitTotalFreeThreshold;" & _
    "dword HeapDeCommitFreeBlockThreshold;" & _
    "dword NumberOfHeaps;" & _
    "dword MaximumNumberOfHeaps;" & _
    "ptr ProcessHeaps;" & _
    "ptr GdiSharedHandleTable;" & _
    "ptr ProcessStarterHelper;" & _
    "ptr GdiDCAttributeList;" & _
    "ptr LoaderLock;" & _
    "dword OSMajorVersion;" & _
    "dword OSMinorVersion;" & _
    "dword OSBuildNumber;" & _
    "dword OSPlatformId;" & _
    "dword ImageSubSystem;" & _
    "dword ImageSubSystemMajorVersion;" & _
    "dword ImageSubSystemMinorVersion;" & _
    "dword GdiHandleBuffer[34];" & _
    "dword PostProcessInitRoutine;" & _
    "dword TlsExpansionBitmap;" & _
    "byte TlsExpansionBitmapBits[128];" & _
    "dword SessionId")

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

    $aCall = DllCall($h_Kernel32DLL, "bool", "ReadProcessMemory", _
    "ptr", $hProcess, _
    "ptr", DllStructGetData($tPROCESS_BASIC_INFORMATION, "PebBaseAddress"), _
    "ptr", DllStructGetPtr($tPEB), _
    "dword", DllStructGetSize($tPEB), _
    "dword*", 0)

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

    If @error Or Not $aCall[0] Then
    DllCall($h_Kernel32DLL, "bool", "CloseHandle", "handle", $hProcess)
    Return SetError(3, 0, "")
    EndIf

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

    Local $tPROCESS_PARAMETERS = DllStructCreate("dword AllocationSize;" & _
    "dword ActualSize;" & _
    "dword Flags;" & _
    "dword Unknown1;" & _
    "word LengthUnknown2;" & _
    "word MaxLengthUnknown2;" & _
    "ptr Unknown2;" & _
    "handle InputHandle;" & _
    "handle OutputHandle;" & _
    "handle ErrorHandle;" & _
    "word LengthCurrentDirectory;" & _
    "word MaxLengthCurrentDirectory;" & _
    "ptr CurrentDirectory;" & _
    "handle CurrentDirectoryHandle;" & _
    "word LengthSearchPaths;" & _
    "word MaxLengthSearchPaths;" & _
    "ptr SearchPaths;" & _
    "word LengthApplicationName;" & _
    "word MaxLengthApplicationName;" & _
    "ptr ApplicationName;" & _
    "word LengthCommandLine;" & _
    "word MaxLengthCommandLine;" & _
    "ptr CommandLine;" & _
    "ptr EnvironmentBlock;" & _
    "dword Unknown[9];" & _
    "word LengthUnknown3;" & _
    "word MaxLengthUnknown3;" & _
    "ptr Unknown3;" & _
    "word LengthUnknown4;" & _
    "word MaxLengthUnknown4;" & _
    "ptr Unknown4;" & _
    "word LengthUnknown5;" & _
    "word MaxLengthUnknown5;" & _
    "ptr Unknown5;")

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

    $aCall = DllCall($h_Kernel32DLL, "bool", "ReadProcessMemory", _
    "ptr", $hProcess, _
    "ptr", DllStructGetData($tPEB, "ProcessParameters"), _
    "ptr", DllStructGetPtr($tPROCESS_PARAMETERS), _
    "dword", DllStructGetSize($tPROCESS_PARAMETERS), _
    "dword*", 0)

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

    If @error Or Not $aCall[0] Then
    DllCall($h_Kernel32DLL, "bool", "CloseHandle", "handle", $hProcess)
    Return SetError(4, 0, "")
    EndIf

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

    $aCall = DllCall($h_Kernel32DLL, "bool", "ReadProcessMemory", _
    "ptr", $hProcess, _
    "ptr", DllStructGetData($tPROCESS_PARAMETERS, "CommandLine"), _
    "wstr", "", _
    "dword", DllStructGetData($tPROCESS_PARAMETERS, "MaxLengthCommandLine"), _
    "dword*", 0)
    If @error Or Not $aCall[0] Then
    DllCall($h_Kernel32DLL, "bool", "CloseHandle", "handle", $hProcess)
    Return SetError(5, 0, "")
    EndIf
    DllCall($h_Kernel32DLL, "bool", "CloseHandle", "handle", $hProcess)
    Return $aCall[3]
    EndFunc ;==>_WinAPI_GetCommandLineFromPID

    [/autoit]
  • Vielen Dank für die schnelle Antwort, lieber AspirinJunkie!

    Das ist ja ein ganz schön kompliziertes Programm.

    Ohne es im Einzelnen zu verstehen, wollte ich es abarbeiten, aber es bringt einen Fehler, weil es z.B. OrOr nicht kennt (siehe Anlage).

    Außerdem wollte ich in der Hilfe nachsehen, was Static bedeutet. Da kommt aber ein Warnhinweis. Was bewirkt Static?

    Für eine weitere Antwort wäre ich Dir sehr dankbar.

  • Scheint ein Problem beim Kopieren gewesen zu sein?
    Den Code der bei dir (zu Recht) den Fehler verursacht habe ich so nicht gepostet.
    Kopiere es noch einmal und teste es nochmal.

    Außerdem wollte ich in der Hilfe nachsehen, was Static bedeutet. Da kommt aber ein Warnhinweis. Was bewirkt Static?

    In einer Funktion werden alle lokalen Variablen bei jedem Aufruf neu erstellt und nach Ende der Funktion wieder vernichtet. Static bewirkt das eine Variable nur beim ersten mal deklariert und definiert wird und dann erhalten bleibt auch nach dem Ende der Funktion.
    Der Vorteil hier besteht darin dass statt bei jedem Funktionsaufruf hier die DLL zu öffnen sie nur einmal am Anfang geöffnet wird und dann für alle nachfolgenden Funktionsaufrufe schon zur Verfügung steht.
    Der Warnhinweis kommt daher da static den Status "experimentell" trägt und die Entwickler daher nicht mit Bugmeldungen dazu überhäuft werden wollen.

  • Nochmals vielen Dank!

    Wenn ich oben auf den Button "Code kopieren" klicke, wird doch tatsächlich so ein Kohl kopiert (siehe Anlage).

    Ich habe das Programm dann über markieren und kopieren übernommen, und jetzt klappt es prima.

    Das ist ja ein schönes Programm. Darf ich die Funktion in mein Programm einbauen?

    Gruß Dieter

  • Verwende einen gescheiten Browser, dann hast das Problem nicht :).

    Andy hat mir ein Schnitzel gebacken aber da war ein Raupi drauf und bevor Oscar das Bugfixen konnte kam Alina und gab mir ein AspirinJunkie.

  • Das ist ja ein schönes Programm. Darf ich die Funktion in mein Programm einbauen?

    Die Funktion ist nicht von mir weswegen ich dort die Quelle mit angegeben habe.
    Die ist von einem User "trancexx" und der hat es in einem öffentlichen Forum gepostet.
    Darfst es also durchaus verwenden aber mach es am besten wie ich und schreib an die Funktion die Quelle dazu.