Jump to content

_ProcessInfo()


Kip
 Share

Recommended Posts

Just an edited example, but all the others were slow, complicated or just for 1 process.

This one does it all in 0.1 seconds :)

The return values:

$Return[0][0] : Count of items

$Return[n][0] : Process name

$Return[n][1] : Process Identifier

$Return[n][2] : Parent Process Identifier

$Return[n][3] : Priority

$Return[n][4] : Process path

$Return[n][5] : Command line

$Return[n][6] : Start date

There are more options available (link), but these are the most used.

#include <Array.au3>
 
 
 Global $ProcessOpen =   _ProcessOpen()
        $SearchList =    _CreateSearchlist("Process","svcHost.exe|explorer.exe|spoolsv.exe")
        $Info =          _ProcessInfo($ProcessOpen,$SearchList)
 
 _ArrayDisplay($Info)
 
 
 
 
; =========================================================================
;
;   _GetProcessInfo($Processopen, $SearchList)
;       
;   Parameters:
;       $Processopen = Vraible from _ProcessOpen()
;       $SearchList  = Array from _CreateSearchlist()
;   Return values:
;       $Return[0][0] = Count of items
;
;       $Return[n][0] = Process name
;       $Return[n][1] = PID
;       $Return[n][2] = Parent PID
;       $Return[n][3] = Priority
;       $Return[n][4] = Process path
;       $Return[n][5] = Commandline
;       $Return[n][6] = Start date
;
; =========================================================================
 
 Func _ProcessInfo($Open, $SearchList=-1)
         
         if not IsArray($SearchList) Then
             local $ProID[1]
                 $ProID[0] = 0
         Else
             $ProID = $SearchList
         EndIf
         
         $O = $Open
         
         Local $Return[1][7]
             $Return[0][0] = 0
         
         Local $OI, $CI = $O.ExecQuery("SELECT * FROM Win32_Process", "WQL", 0x30)
         For $OI In $CI
             
             $pProcess = $OI.Name
             
             $pPID = $OI.ProcessID
             $pParentPID = $OI.ParentProcessId
             
             $pPriority = $OI.Priority
             
             $pPath = $OI.ExecutablePath
             $pCommandline = StringReplace($OI.CommandLine,"""","")
             
             $pStarted = $OI.Creationdate
 
             If $ProID[0] > 0 Then
                 if $ProID[1] = "PID" Then
                     for $i = 2 to $ProID[0]+1
                         if $ProID[$i] = $pPID Then
                             
                             $Ubound = UBound($Return)
                             ReDim $Return[$Ubound+1][7]
                             $Ubound = UBound($Return)
                             
                             $Return[$Ubound-1][0] = $pProcess
                             
                             $Return[$Ubound-1][1] = $pPID
                             $Return[$Ubound-1][2] = $pParentPID
                         
                             $Return[$Ubound-1][3] = $pPriority
                             
                             $Return[$Ubound-1][4] = $pPath
                             $Return[$Ubound-1][5] = $pCommandline
                             
                             If $pStarted Then
                                 $Year = StringMid($pStarted, 1, 4)
                                 $Month = StringMid($pStarted, 5, 2)
                                 $Day = StringMid($pStarted, 7, 2)
                             
                                 $Hour = StringMid($pStarted, 9, 2)
                                 $Minute = StringMid($pStarted, 11, 2)
                                 $Second = StringMid($pStarted, 13, 2)
                                 
                                 $Return[$Ubound-1][6] = $Year &":"& $Month &":"& $Day &" "& $Hour &":"& $Minute &":"& $Second
                             Else
                                 $Return[$Ubound-1][6] = 0
                             EndIf
                         
                             $Return[0][0] += 1
                             
                         EndIf
                     Next
                 EndIf
                 
                 if $ProID[1] = "PrN" Then
                     for $i = 2 to $ProID[0]+1
                         
                         if $ProID[$i] = $pProcess Then
                             
                             $Ubound = UBound($Return)
                             ReDim $Return[$Ubound+1][7]
                             $Ubound = UBound($Return)
                             
                             $Return[$Ubound-1][0] = $pProcess
                             
                             $Return[$Ubound-1][1] = $pPID
                             $Return[$Ubound-1][2] = $pParentPID
                         
                             $Return[$Ubound-1][3] = $pPriority
                             
                             $Return[$Ubound-1][4] = $pPath
                             $Return[$Ubound-1][5] = $pCommandline
                             
                             If $pStarted Then
                                 $Year = StringMid($pStarted, 1, 4)
                                 $Month = StringMid($pStarted, 5, 2)
                                 $Day = StringMid($pStarted, 7, 2)
                             
                                 $Hour = StringMid($pStarted, 9, 2)
                                 $Minute = StringMid($pStarted, 11, 2)
                                 $Second = StringMid($pStarted, 13, 2)
                                 
                                 $Return[$Ubound-1][6] = $Year &":"& $Month &":"& $Day &" "& $Hour &":"& $Minute &":"& $Second
                             Else
                                 $Return[$Ubound-1][6] = 0
                             EndIf
                         
                             $Return[0][0] += 1
                             
                         EndIf
                     Next
                 EndIf
                 
             Else
                 
                 If $pProcess Or $pPID or $pPath or $pCommandline or $pParentPID Then
                     $Ubound = UBound($Return)
                     ReDim $Return[$Ubound+1][7]
                     $Ubound = UBound($Return)
                     
                     $Return[$Ubound-1][0] = $pProcess
                     
                     $Return[$Ubound-1][1] = $pPID
                     $Return[$Ubound-1][2] = $pParentPID
                 
                     $Return[$Ubound-1][3] = $pPriority
                     
                     $Return[$Ubound-1][4] = $pPath
                     $Return[$Ubound-1][5] = $pCommandline
                     
                     If $pStarted Then
                         $Year = StringMid($pStarted, 1, 4)
                         $Month = StringMid($pStarted, 5, 2)
                         $Day = StringMid($pStarted, 7, 2)
                     
                         $Hour = StringMid($pStarted, 9, 2)
                         $Minute = StringMid($pStarted, 11, 2)
                         $Second = StringMid($pStarted, 13, 2)
                         
                         $Return[$Ubound-1][6] = $Year &":"& $Month &":"& $Day &" "& $Hour &":"& $Minute &":"& $Second
                     Else
                         $Return[$Ubound-1][6] = 0
                     EndIf
                 
                     $Return[0][0] += 1
                 EndIf
             EndIf
         Next
         
         Return $Return
 EndFunc
 
 
; =========================================================================
;
;   _CreateSearchlist($Type,$Items)
;       
;   Parameters:
;       $Type = Vraible from _ProcessOpen()
;       $Items  = Array from _CreateSearchlist()
;   Return values:
;       $Return = Array that can be used for _processInfo()
;
; =========================================================================
 
 Func _CreateSearchlist($Type,$Items)
     Local $Return[2]
         $Return[0] = 0
     if $Type = "PID" Then $Return[1] = "PID"
     if $Type = "Process" Then $Return[1] = "PrN"
     
     $Split = StringSplit($Items,"|")
     
     for $i = 1 to $Split[0]
         ReDim $Return[UBound($Return)+1]
             $Return[UBound($Return)-1] = $Split[$i]
             $Return[0] += 1
     Next
     
     Return $Return
     
 EndFunc
 
 Func _ProcessOpen()
     $O = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2"); This only has to be called once. If its called in a loop, it would slow the script down (a little bit)
     Return $O
     
 EndFunc
1.1 Edited by kip
Link to comment
Share on other sites

For newbies:

#include <Array.au3>
$Process =     _ProcessOpen()
$Info =     _ProcessInfo($Process)

_ArrayDisplay($Info)

MsgBox(0, "Test",$Info[5][4])

$Info[5][4]

5 is the Row nr & 4 is the column name

Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...