Jump to content

Compare 2 ProcessList arrays (especially PIDs) and kill those absent in first list


Recommended Posts

Hello, I don't know if I'm really clear on what I am asking. I did a search and didn't find what I'm looking for or my logic don't allow me to find out the answers.

I am doing a little program for students that are taking exams. What I want to do is start an exam for the student and take the running processlist (especially the PIDs). I throw everything inside an array and during the exam, I take again all the running processlist and throw it inside a second array.

After, I want to compare the two arrays and kill each process that appears in the second arrays that are not in the first arrays.

Right now, what I did is :

#include <Array.au3>

$ProcessList1 = ProcessList()
$NumberOfProcess = $ProcessList1[0][0]

While 1
    For $j = 0 To 100
        $ProcessList2 = ProcessList()
        $NumberOfProcess2 = $ProcessList2[0][0]
        For $i = 0 To UBound($ProcessList2)-1
            $found = _ArraySearch($ProcessList1, $ProcessList2[$i][1])
            ProcessClose($found)
        Next
    Next
Wend

I know this is not working but am I using the good logic to compare the two and kill differences? What can I do to make this code works?

Thanks a lot.

Link to comment
Share on other sites

Something like this ?

;////////////////////////////////////////////////////////////////////
;//   Watchdog  - kill not allowed programs
;////////////////////////////////////////////////////////////////////
#NoTrayIcon
HotKeySet("+!b", "Terminate")  ;Shift-Alt-d
;////////////////////////////////////////////////////////////////////
;//   ONLY ONE SESSION AT TIME
;////////////////////////////////////////////////////////////////////
$list = ProcessList(@ScriptName)
for $i = 1 to $list[0][0]
    if $i > 1 Then
        msgbox(0, "Alert!", "Only one instance is allowed : " & @ScriptName, 1)
        ProcessClose($list[$i][1])
    EndIf
next

;//////////////////////////////////////////////////////////////////////
;//      MAIN
;//////////////////////////////////////////////////////////////////////

if FileExists("allowed.ini") then       ;if .ini exists kill other process
    While 1
        XenoKiller()
        sleep(1000)
    WEnd
else
    StoreRunning()                      ;if .ini NOT exists create file/list 
EndIf




;//////////////////////////////////////////////////////////////////////
;//      FUNCTIONS
;//////////////////////////////////////////////////////////////////////
;//      FIRST RUN create allowed .exe list
;//////////////////////////////////////////////////////////////////////
Func StoreRunning($file = "allowed.ini")
    ; List all processes in a File
    local $list = ProcessList()
    
    for $i = 1 to $list[0][0]
        FileWriteLine("allowed.ini", $list[$i][0] & @CRLF)
    next
    msgbox(0, "First Run", "OK, i've created allowed.ini that contains allowed .exe." & @CRLF & "Remember ! SHIFT+Alt+d to quit " & @ScriptName)
EndFunc

;/////////////////////////////////////////////////////////////
;//      KILL process not in list
;/////////////////////////////////////////////////////////////
Func XenoKiller()
    ; List all processes
    local $list = ProcessList()

    ;/////////////////////////////////////////////////////////////
    ;//      FOR to find running .exe in my list
    ;/////////////////////////////////////////////////////////////
    for $i = 1 to $list[0][0]
        Local $linenum = 0                      ;This is for skip line in filereadline function
        $file = FileOpen("allowed.ini", 0)      ;open allowed list
        If $file = -1 Then                      ; Check if file opened for reading OK
            MsgBox(0, "Error", "Unable to open file.")
            Exit
        EndIf
        ;/////////////////////////////////////////////////////////////
        ;//      PROCESS = line read  ?
        ;/////////////////////////////////////////////////////////////
        While 1
            $linenum = $linenum + 1                 ;add one to skip next line
            $line = FileReadLine($file, $linenum)   ;each cicle read next line
            if $list[$i][0] = $line Then ExitLoop   ;if process name is in my list it's safe, exit to next one
            If @error = -1 Then                     ;if EOF , I can't find in my list
                $PID = ProcessExists($list[$i][0]) ; Will return the PID or 0 if the process isn't found.
                If $PID Then ProcessClose($PID)
                ExitLoop                        ;and exit to next one
            EndIf
        Wend
        FileClose($file)
    next
EndFunc

Func Terminate()
    ToolTip("ok, quit " & @ScriptName, 0, 0)
    Sleep(2000)
    Beep(500,250)
    Beep(1000,250)
    Beep(500,250)
    Exit 0
EndFunc
Link to comment
Share on other sites

kornboy82,

A better logic would be :

#include <Array.au3>

$ProcessList1 = ProcessList()
Run("notepad.exe")
$ProcessList2 = ProcessList()
ProcessClose("notepad.exe")

For $i = 1 To $ProcessList2[0][0]
      $found = _ArraySearch($ProcessList1, $ProcessList2[$i][1], 1, 0, 0, 0, 1, 1)
      If $found = -1 Then Msgbox(0,"", "intruder was : " & $ProcessList2[$i][0])  ; ProcessClose($ProcessList2[$i][1])
Next

Edit

And like this for a continuous check :

#include <Array.au3>
#include <MsgBoxConstants.au3>

$ProcessList1 = ProcessList()

While 1
  $ProcessList2 = ProcessList()
  For $i = 1 To $ProcessList2[0][0]
      $found = _ArraySearch($ProcessList1, $ProcessList2[$i][1], 1, 0, 0, 0, 1, 1)
      If $found = -1 Then Msgbox($MB_TOPMOST,"", "intruder was : " & $ProcessList2[$i][0])  ; ProcessClose($ProcessList2[$i][1])
  Next
  Sleep(100)
Wend
Edited by mikell
Link to comment
Share on other sites

 

kornboy82,

A better logic would be :

#include <Array.au3>

$ProcessList1 = ProcessList()
Run("notepad.exe")
$ProcessList2 = ProcessList()
ProcessClose("notepad.exe")

For $i = 1 To $ProcessList2[0][0]
      $found = _ArraySearch($ProcessList1, $ProcessList2[$i][1], 1, 0, 0, 0, 1, 1)
      If $found = -1 Then Msgbox(0,"", "intruder was : " & $ProcessList2[$i][0])  ; ProcessClose($ProcessList2[$i][1])
Next

Edit

And like this for a continuous check :

#include <Array.au3>
#include <MsgBoxConstants.au3>

$ProcessList1 = ProcessList()

While 1
  $ProcessList2 = ProcessList()
  For $i = 1 To $ProcessList2[0][0]
      $found = _ArraySearch($ProcessList1, $ProcessList2[$i][1], 1, 0, 0, 0, 1, 1)
      If $found = -1 Then Msgbox($MB_TOPMOST,"", "intruder was : " & $ProcessList2[$i][0])  ; ProcessClose($ProcessList2[$i][1])
  Next
  Sleep(100)
Wend

 

Thank you, this is exactly what I was looking for. I was not far but needed a little push to help me.

Link to comment
Share on other sites

Glad I could push  :)

 

And also, the code works perfectly for our needs. I just changed the message box for ProcessClose($ProcessList2[$i][1]) to make it close processes by it's IDs so that way, if the person opens another instance of Microsoft Word per example, it closes only the new one and not the one opened.

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