Sign in to follow this  
Followers 0
Hooch

Possible To Get A Process Owner?

9 posts in this topic

Hey all, I need to write a script to shut down a java.exe process. The problem is I can't use a shotgun and take out any and all java.exe processes, I need to kill the one owned by a particular user (dummy batch process id)

Looking at the available functions I don't see a way to access that information. A forum search was unenlightening.

Thoughts?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hey all, I need to write a script to shut down a java.exe process. The problem is I can't use a shotgun and take out any and all java.exe processes, I need to kill the one owned by a particular user (dummy batch process id)

Looking at the available functions I don't see a way to access that information. A forum search was unenlightening.

Thoughts?

I helped work on a function to get the full path to the exe file, could you get the file information you need by the file/folder location?

Edit:

Here's the link just in case: http://www.autoitscript.com/forum/index.ph...ndpost&p=165980

Larry actually has wrote the exe full path part I believe.

Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

Yup that would suffice!

Share this post


Link to post
Share on other sites

Yup that would suffice!

Edited above with link.

[center]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.[/center]

Share this post


Link to post
Share on other sites

mmm works nicely except, unlike the topic of that thread, the process I need access to is not visible as it is a background java process, no window.... :think:

Perhaps I can massage it..

Share this post


Link to post
Share on other sites

mmm works nicely except, unlike the topic of that thread, the process I need access to is not visible as it is a background java process, no window.... :think:

Perhaps I can massage it..

The core functions you need to look at are the comments ----------Larry's code below--------- he actually wrote exactly what you need, and you can just mod the functions from there... maybe something like ProcessLists('Java.exe') and go throught only the java exe's that you need... let me know if you need a hand with it... could probably whip it up in a few minutes for ya.

[center]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.[/center]

Share this post


Link to post
Share on other sites

Had an obvious error in my last post, so here is a script that will look for a process running from a specified directory and terminate it. This version only deals with java.exe processes though...

;

$killMe = "path\to\my\process.exe"
$grossProcessList = _runningEXE()
$myProcessList = ''
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

;

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

;

Share this post


Link to post
Share on other sites

thanks alot. this helped me alot

Share this post


Link to post
Share on other sites

If just the process name known, then try this function (i am put together a couple functions from this forum and wrote one of my owne :whistle: ):

_ProcessCloseByOwner("Java.exe", "All")

Func _ProcessCloseByOwner($sProcess, $Owner)
    If Not ProcessExists($sProcess) Then Return SetError(1)
    Local $i = 1, $PrcObj = "", $ExitLoop = 0, $Pid, $ExecPath, $objItem
    Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    Local $PrcObj = $objWMIService.ExecQuery ("SELECT * FROM Win32_Process", "WQL", 0x30)
    Local $ProcList = ProcessList($sProcess)

    While 1
        $Pid = $ProcList[$i][1]
        If IsObj($PrcObj) Then
            For $objItem In $PrcObj
                If Int($objItem.ProcessId) = $Pid Then
                    $ExecPath = $objItem.ExecutablePath
                    If Not $ExecPath Then $ExecPath = _PathSearch($objItem.Caption)
                    If FileExists($ExecPath) Then
                        If _GetExtProperty($ExecPath, 8) = $Owner Then
                            ProcessClose($Pid)
                            If ProcessExists($Pid) Then ProcessClose($objItem.Caption)
                            $ExitLoop = 1
                            ExitLoop
                        EndIf
                    EndIf
                EndIf
            Next
        EndIf
        $i += 1
        If $ExitLoop Or $i >= $ProcList[0][0] Then ExitLoop
    WEnd
EndFunc

Func _GetExtProperty($sPath, $iProp)
    Local $iExist, $sFile, $sDir, $oShellApp, $oDir, $oFile, $aProperty, $sProperty
    $iExist = FileExists($sPath)
    If $iExist = 0 Then
        SetError(1)
        Return 0
    Else
        $sFile = StringTrimLeft($sPath, StringInStr($sPath, "\", 0, -1))
        $sDir = StringTrimRight($sPath, (StringLen($sPath) - StringInStr($sPath, "\", 0, -1)))
        $oShellApp = ObjCreate ("shell.application")
        $oDir = $oShellApp.NameSpace ($sDir)
        $oFile = $oDir.Parsename ($sFile)
        If $iProp = -1 Then
            Local $aProperty[35]
            For $i = 0 To 34
                $aProperty[$i] = $oDir.GetDetailsOf ($oFile, $i)
            Next
            Return $aProperty
        Else
            $sProperty = $oDir.GetDetailsOf ($oFile, $iProp)
            If $sProperty = "" Then
                Return 0
            Else
                Return $sProperty
            EndIf
        EndIf
    EndIf
EndFunc

Func _PathSearch($ProcName)
    Local $PathArr = StringSplit(EnvGet("PATH"), ";"), $iN, $CurrentSearchPath
    If IsArray($PathArr) Then
        For $iN = 1 To $PathArr[0]
            $CurrentSearchPath = $PathArr[$iN] & "\" & $ProcName
            If FileExists($CurrentSearchPath) Then Return $CurrentSearchPath
        Next
    EndIf
    Return $ProcName
EndFunc

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

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  
Followers 0