Jump to content
Sign in to follow this  

Acquiring The Owner Of A Process

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

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]      

;~ 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)
    Return StringSplit(StringTrimRight($a_DummyProcessList, 1), Chr(01))


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


Share this post

Link to post
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
Sign in to follow this