Jump to content

Acquiring The Owner Of A Process


 Share

Recommended Posts

Ok, what I want to do:

Get a list of running processes, identify the java process running as a particular user, lets say "steve", and then kill it.

I was unable to find a way to get the owner of a process so I took a suggestion from the forum and tried to filter it by the directory the process is running out of.

This seemed to be a good approach as when I was testing it seemed to work. Now however the same kind of testing is showing the process running from the home dir of the JRE and not where the jar file is located.

Why this behaivor changed I am not sure but either way I have to deal with it.

So below is my current code, if anyone has any ideas I am all ears...

;=====================================
;~ Runtime
;~ Aquire a list of all running java processes
;~ Isolate the ones that are run from $killMe 
;~ ("C:\home\nbatch\PROD\batchExecutable")
;~ Terminate the process
;=====================================
#include <Array.au3>
$killMe = "C:\home\nbatch\PROD\batchExecutable"
$grossProcessList = _runningEXE()
$myProcessList = ''

;~ _ArrayDisplay ( $grossProcessList, "Array Display" )
For $i = 1 To UBound($grossProcessList) - 1
    if StringInStr($grossProcessList[$i], "java.exe") then
        $myProcessList = $myProcessList & $grossProcessList[$i] & @LF
    endIf
Next

local $myProcessArray = StringSplit($myProcessList, @LF)
For $i = 1 to UBound($myProcessArray) - 1   
    If StringInStr($myProcessArray[$i], $killMe) Then
        local $thisProcess = $myProcessArray[$i]
        local $myPIDstring = StringSplit($thisProcess, "|")
        local $myPID = $myPIDstring[2]      
        ProcessClose($myPID)
    endIf
Next

;=====================================
;~ Functions
;=====================================

Func _runningEXE()
    Local $a_ProcessList = ProcessList()
    Local $a_DummyProcessList  
    For $iProcessList = 1 To $a_ProcessList[0][0]   
        $a_DummyProcessList = $a_DummyProcessList & GetProcessName($a_ProcessList[$iProcessList][1]) & "|" & $a_ProcessList[$iProcessList][1] & Chr(01)
    Next
    Return StringSplit(StringTrimRight($a_DummyProcessList, 1), Chr(01))
EndFunc

;

Func GetProcessName(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

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