Jump to content

Maybe A Handy Function For U


Amen
 Share

Recommended Posts

A Handy Function for me,maybe for u.An example to test it

$List=_ProcessInit()
    If @error then 
        MsgBox(0,"","Could't get proccess List "&@ScriptName&"aborted!")
        Exit
    EndIf
    sleep(2000)

While 1
    $msg=_ProcessDiff($List)
    If   $msg<>"" then MsgBox(0,"New Processes since Last _ProccessInit",$msg)
    sleep(3000)
wend

Exit

;==================================================================================
;
; Function Name:  _ProcessInit()
; Description:      
;                    Success: An array of process names and PIDs 
;                           Uses ProcessList() function. See Help for ProccessList
;                   Failure: @error is set to 1
; Author(s):      Amen
;
;==================================================================================
Func _ProcessInit()
$Lst=ProcessList()
If @error then return @error
return $Lst
EndFunc 

;==================================================================================
;
; Function Name:  _ProcessDiff()
; Description:      Returns new Proccesses since previous call to _ProccessInit()
;                   Uses ProcessList() function
;                    Success: A string with Proccess names seperated with pipe (|)                          
;                   Failure: @error is set to 1
;                            Returns ""
; Author(s):      Amen
;
;==================================================================================
Func _ProcessDiff($Array)
local $lst2,$i,$i2,$Found,$StringList
    $Lst2=ProcessList()
    If @error then 
        return  ""
    Else
        $StringList=""
        for $i = 1 to $lst2[0][0]
            $Found=""
                For $i2=1 to $Array[0][0]
                    If $Array[$i2][0]=$lst2[$i][0] then 
                        $Found=1;$Found&$lst2[$i][0]
                        ExitLoop
                    EndIf
                Next
            If $Found="" then 
                If $StringList="" then 
                    $StringList=$lst2[$i][0]
                Else
                    $StringList=$lst2[$i][0]&"|"&$StringList
                EndIf
            EndIf
        next
     return $StringList
    EndIf
EndFunc
Edited by Amen
Link to comment
Share on other sites

  • Moderators

$List=_ProcessInit()
    If @error then
        MsgBox(0,"","Could't get proccess List "&@ScriptName&"aborted!")
        Exit
    EndIf
    sleep(2000)
Func _ProcessInit()
$Lst=ProcessList()
If @error then return @error
return $Lst
EndFunc
Kind of redundant isn't it?

$List = ProcessList()
If @error Then
    MsgBox(0,"","Could't get proccess List "&@ScriptName&"aborted!")
    Exit
EndIf
Wouldn't this be the same?

Also, there was some work done on this a while ago here: http://www.autoitscript.com/forum/index.ph...ndpost&p=139602

If you wanted to add or take away a bit...

Nice job.

Edit:Thought I'd revamp mine:

Dim $GetProcess , $CompareProcess, $TestThis, $StringArray
Dim $FirstArray = FirstProcessCall()
If $FirstArray == 1 Then 
    MsgBox(64, 'Error', 'Problem reading process list')
EndIf

Local $GetProcessTime = TimerInit()
While 1
    If TimerDiff($GetProcessTime) / 1000 >= 1 Then; checks for a change in process list ever 1 second
        Local $GotList = GetProccessInfo($FirstArray)
        If $GotList == 1 Then
            MsgBox(64, 'Error', 'Wrong value set for keeping the old or new process list')
        ElseIf $GotList == 2 Then
            MsgBox(64, 'Change', 'The Process List Changed')
        ElseIf $GotList == 3 Then
            MsgBox(64, 'Change', 'The Process List Changed' & @CR & @CR & 'Now Exiting')
            Exit; Didn't want to continue with new list, no sense in repeating the function.
        EndIf
        $GetProcessTime = TimerInit()
    EndIf
    Sleep(100)
WEnd

Func FirstProcessCall()
    Local $a_FirstProcessCall = ProcessList()
    Local $a_StringArray = ''
    If @error Then Return 1
    For $i = 1 To $a_FirstProcessCall[0][0]
        $a_StringArray = $a_StringArray & $a_FirstProcessCall[$i][0] & Chr(01)
    Next
    Return $a_StringArray
EndFunc

Func GetProccessInfo($a_LastArray, $i_KeepOld = 0)
    If $i_KeepOld <> 0 And $i_KeepOld <> 1 Then Return 1; error setting that it wasn't a 1 or 0 chosen
    Local $n_NowList = ProcessList()
    Local $r_ReturnArray = ''
    Local $d_Delimeter = Chr(01)
    For $i = 1 To $n_NowList[0][0]
        $r_ReturnArray = $r_ReturnArray & $n_NowList[$i][0] & $d_Delimeter
    Next
    If Not StringInStr($a_LastArray, $r_ReturnArray) Then
        If $i_KeepOld = 1 Then
            Return 2
        Else
            $FirstArray = $r_ReturnArray
            Return 3
        EndIf
    EndIf
EndFunc
Might find the speed a bit better this way :) Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Yea the main code of my script can be done better.it was a quick examle how to use the two functions..

Thanx for the comment and your link.i'm working on..if i can add or use smthing

Oh by the way.is there any way to get the Applications Running? not the processlist!

Is in probably on the text of Taskbar?

And one other thing.How can i take the full path of a process running?

Edited by Amen
Link to comment
Share on other sites

  • Moderators

Yea the main code of my script can be done better.it was a quick examle how to use the two functions..

Thanx for the comment and your link.i'm working on..if i can add or use smthing

Oh by the way.is there any way to get the Applications Running? not the processlist!

Is in probably on the text of Taskbar?

And one other thing.How can i take the full path of a process running?

Do you mean visible windows applications running (like the exe to the visible window)?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

If we press alt Ctrl del on Windows and go to task manager there is a tab called

Applications.This one. Not the list of tab processes.

I wonna make o prog that will log all applications a user runned.Log must be easy to read from a non advanced user(a person that dont know too much from proccesses etc).

How to get The full path from a proccess running is another question.Has nothing to do with the above

Edited by Amen
Link to comment
Share on other sites

  • Moderators

This may help with the first question:

MsgBox(0, 'Test', _VisibleWindowEXE())

Func _VisibleWindowEXE()
    Local $a_WinList = WinList()
    Local $a_DummyWinList = ''
    Local $a_ProcessList = ProcessList()
    Local $a_DummyProcessList
    For $iWinList = 1 To $a_WinList[0][0]
        If $a_WinList[$iWinList][0] <> '' And BitAND(WinGetState($a_WinList[$iWinList][1]), 2) Then
            $a_DummyWinList = $a_DummyWinList & $a_WinList[$iWinList][0] & Chr(01)
        EndIf
    Next
    $a_DummyWinList = StringSplit(StringTrimRight($a_DummyWinList, 1), Chr(01))
    For $iProcessList = 1 To $a_ProcessList[0][0]
        For $iWinList = 1 To UBound($a_DummyWinList) - 1
            If WinGetProcess($a_DummyWinList[$iWinList]) = $a_ProcessList[$iProcessList][1] _ 
                And Not StringInStr($a_ProcessList[$iProcessList][0], 'explorer.exe') Then
                $a_DummyProcessList = $a_DummyProcessList & $a_ProcessList[$iProcessList][0] & @LF
            EndIf
        Next
    Next
    Return $a_DummyProcessList
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Here, I mixed Larry's with mine, and this is what you get:

MsgBox(0, 'Test', _VisibleWindowEXE())

Func _VisibleWindowEXE()
    Local $a_WinList = WinList()
    Local $a_DummyWinList = ''
    Local $a_ProcessList = ProcessList()
    Local $a_DummyProcessList
    For $iWinList = 1 To $a_WinList[0][0]
        If $a_WinList[$iWinList][0] <> '' And BitAND(WinGetState($a_WinList[$iWinList][1]), 2) Then
            $a_DummyWinList = $a_DummyWinList & $a_WinList[$iWinList][0] & Chr(01)
        EndIf
    Next
    $a_DummyWinList = StringSplit(StringTrimRight($a_DummyWinList, 1), Chr(01))
    For $iProcessList = 1 To $a_ProcessList[0][0]
        For $iWinList = 1 To UBound($a_DummyWinList) - 1
            If WinGetProcess($a_DummyWinList[$iWinList]) = $a_ProcessList[$iProcessList][1] _ 
                And Not StringInStr($a_ProcessList[$iProcessList][0], 'explorer.exe') Then
                $a_DummyProcessList = $a_DummyProcessList & GetProcessName($a_ProcessList[$iProcessList][1]) & @LF
            EndIf
        Next
    Next
    Return $a_DummyProcessList
EndFunc

;--------------------------This from Larry from here down!! ---------------------------------
Func GetProcessName(ByRef $PID)
    Local $ret
    If @OSTYPE = "WIN32_WINDOWS" Then
        $ret = GetProcessName95($PID)
        SetError(@error)
        Return $ret
    Else
        $ret = GetProcessNameNT($PID)
        SetError(@error)
        Return $ret
    EndIf
EndFunc

Func GetProcessName95(ByRef $PID)
    Const $TH32CS_SNAPPROCESS = 0x00000002
    Const $INVALID_HANDLE_VALUE = -1
    Local $ToolHandle, $Process, $nRet

    $ToolHandle = DLLCall("kernel32.dll","hwnd","CreateToolhelp32Snapshot","int",$TH32CS_SNAPPROCESS,"int",0)
    If $ToolHandle[0] = $INVALID_HANDLE_VALUE Then Return ("" & SetError(1))
    
    $Process = DLLStructCreate("int;int;int;uint;int;int;int;int;int;char[260]")
    DllStructSetData($Process,1,DLLStructGetSize($Process))

    $nRet = DLLCall("kernel32.dll","int","Process32First","hwnd",$ToolHandle[0],"ptr",DllStructGetPtr($Process))
    If $nRet[0] Then
        Do
            If DLLStructGetData($Process,3) = $PID Then
                $nRet = DllStructGetData($Process,10)
                $Process = 0
                Return $nRet
            EndIf
            $nRet = DLLCall("kernel32.dll","int","Process32Next","hwnd",$ToolHandle[0],"ptr",DllStructGetPtr($Process))
        Until Not $nRet[0]
    EndIf
    $Process = 0
    DLLCall("kernel32.dll","int",$ToolHandle[0])
    SetError(1)
    Return
EndFunc

Func GetProcessNameNT(ByRef $PID)
    Local $Process, $Modules, $Ret
    Const $PROCESS_QUERY_INFORMATION = 0x0400
    Const $PROCESS_VM_READ = 0x0010
    $Process = DLLCall("kernel32.dll","hwnd","OpenProcess","int", _
            BitOR($PROCESS_QUERY_INFORMATION,$PROCESS_VM_READ),"int",0,"int",$PID)
    If $Process[0] = 0 Then Return SetError(1)
    
    $Modules = DLLStructCreate("int[1024]")
    DLLCall("psapi.dll","int","EnumProcessModules","hwnd",$Process[0],"ptr",DllStructGetPtr($Modules), _
            "int",DllStructGetSize($Modules),"int_ptr",0)
    
    $Ret = DLLCall("psapi.dll","int","GetModuleFileNameEx","hwnd",$Process[0],"int",DllStructGetData($Modules,1), _
            "str","","int",2048)
    $Modules = 0
    If StringLen($Ret[3]) = 0 Then Return SetError(1)
    Return $Ret[3]
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Ok, my last contribution... Test Code

$Test = _VisibleWindowEXE(1, 1); Not full path and no repeat exe's
$CreateList = ''
For $i = 1 To UBound($Test) - 1
    $CreateList = $CreateList & $Test[$i] & @LF
Next
MsgBox(0, 'Show Array 1', $CreateList)

$Test2 = _VisibleWindowEXE(0, 1); Full Path and no repeat exe's
$CreateList = ''
For $i = 1 To UBound($Test2) - 1
    $CreateList = $CreateList & $Test2[$i] & @LF
Next
MsgBox(0, 'Show Array 2', $CreateList)

$Test3 = _VisibleWindowEXE(); Full Path and all visible window exe's
$CreateList = ''
For $i = 1 To UBound($Test3) - 1
    $CreateList = $CreateList & $Test3[$i] & @LF
Next
MsgBox(0, 'Show Array 3', $CreateList)

Include Code

Func _VisibleWindowEXE($i_FullPath = 0, $i_RepeatEXENames = 0)
    If ($i_RepeatEXENames <> 0 And $i_RepeatEXENames <> 1) Or ($i_FullPath <> 0 And $i_FullPath <> 1) Then Return 1; $i_RepeatEXENames and $i_FullPath must be a 1 or 0
    Local $a_WinList = WinList()
    Local $a_DummyWinList = ''
    Local $a_ProcessList = ProcessList()
    Local $a_DummyProcessList
    For $iWinList = 1 To $a_WinList[0][0]
        If $a_WinList[$iWinList][0] <> '' And BitAND(WinGetState($a_WinList[$iWinList][1]), 2) Then
            $a_DummyWinList = $a_DummyWinList & $a_WinList[$iWinList][0] & Chr(01)
        EndIf
    Next
    $a_DummyWinList = StringSplit(StringTrimRight($a_DummyWinList, 1), Chr(01))
    For $iProcessList = 1 To $a_ProcessList[0][0]
        For $iWinList = 1 To UBound($a_DummyWinList) - 1
            If WinGetProcess($a_DummyWinList[$iWinList]) = $a_ProcessList[$iProcessList][1] _ 
                And Not StringInStr($a_ProcessList[$iProcessList][0], 'explorer.exe') Then
                If $i_RepeatEXENames = 0 And $i_FullPath = 0 Then
                    $a_DummyProcessList = $a_DummyProcessList & GetProcessName($a_ProcessList[$iProcessList][1]) & Chr(01)
                ElseIf $i_RepeatEXENames = 1 And $i_FullPath = 0 Then
                    If Not StringInStr(Chr(01) & $a_DummyProcessList, Chr(01) & GetProcessName($a_ProcessList[$iProcessList][1]) & Chr(01)) Then
                        $a_DummyProcessList = $a_DummyProcessList & GetProcessName($a_ProcessList[$iProcessList][1]) & Chr(01)
                    EndIf
                ElseIf $i_RepeatEXENames = 0 And $i_FullPath = 1 Then
                    $a_DummyProcessList = $a_DummyProcessList & $a_ProcessList[$iProcessList][0] & Chr(01)
                Else
                    If Not StringInStr(Chr(01) & $a_DummyProcessList, Chr(01) & $a_ProcessList[$iProcessList][0] & Chr(01)) Then
                        $a_DummyProcessList = $a_DummyProcessList & $a_ProcessList[$iProcessList][0] & Chr(01)
                    EndIf
                EndIf
            EndIf
        Next
    Next
    Return StringSplit(StringTrimRight($a_DummyProcessList, 1), Chr(01))
EndFunc

;--------------------------This from Larry from here down!! ---------------------------------
Func GetProcessName(ByRef $PID)
    Local $ret
    If @OSTYPE = "WIN32_WINDOWS" Then
        $ret = GetProcessName95($PID)
        SetError(@error)
        Return $ret
    Else
        $ret = GetProcessNameNT($PID)
        SetError(@error)
        Return $ret
    EndIf
EndFunc

Func GetProcessName95(ByRef $PID)
    Const $TH32CS_SNAPPROCESS = 0x00000002
    Const $INVALID_HANDLE_VALUE = -1
    Local $ToolHandle, $Process, $nRet

    $ToolHandle = DLLCall("kernel32.dll","hwnd","CreateToolhelp32Snapshot","int",$TH32CS_SNAPPROCESS,"int",0)
    If $ToolHandle[0] = $INVALID_HANDLE_VALUE Then Return ("" & SetError(1))
    
    $Process = DLLStructCreate("int;int;int;uint;int;int;int;int;int;char[260]")
    DllStructSetData($Process,1,DLLStructGetSize($Process))

    $nRet = DLLCall("kernel32.dll","int","Process32First","hwnd",$ToolHandle[0],"ptr",DllStructGetPtr($Process))
    If $nRet[0] Then
        Do
            If DLLStructGetData($Process,3) = $PID Then
                $nRet = DllStructGetData($Process,10)
                $Process = 0
                Return $nRet
            EndIf
            $nRet = DLLCall("kernel32.dll","int","Process32Next","hwnd",$ToolHandle[0],"ptr",DllStructGetPtr($Process))
        Until Not $nRet[0]
    EndIf
    $Process = 0
    DLLCall("kernel32.dll","int",$ToolHandle[0])
    SetError(1)
    Return
EndFunc

Func GetProcessNameNT(ByRef $PID)
    Local $Process, $Modules, $Ret
    Const $PROCESS_QUERY_INFORMATION = 0x0400
    Const $PROCESS_VM_READ = 0x0010
    $Process = DLLCall("kernel32.dll","hwnd","OpenProcess","int", _
            BitOR($PROCESS_QUERY_INFORMATION,$PROCESS_VM_READ),"int",0,"int",$PID)
    If $Process[0] = 0 Then Return SetError(1)
    
    $Modules = DLLStructCreate("int[1024]")
    DLLCall("psapi.dll","int","EnumProcessModules","hwnd",$Process[0],"ptr",DllStructGetPtr($Modules), _
            "int",DllStructGetSize($Modules),"int_ptr",0)
    
    $Ret = DLLCall("psapi.dll","int","GetModuleFileNameEx","hwnd",$Process[0],"int",DllStructGetData($Modules,1), _
            "str","","int",2048)
    $Modules = 0
    If StringLen($Ret[3]) = 0 Then Return SetError(1)
    Return $Ret[3]
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

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...