Sign in to follow this  
Followers 0
SimV

Get HANDLES of process

11 posts in this topic

Hello!

Is it possible to get handles of a single running process?

Thanks

Share this post


Link to post
Share on other sites



Hello!

Is it possible to get handles of a single running process?

Thanks

This is an cmd example:

wmic process get handlecount,description

Description HandleCount

System Idle Process 0

System 2085

winlogon.exe 618

services.exe 375

lsass.exe 582

svchost.exe 100

[..]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

ProcessGetList()

The array returned is two-dimensional and is made up as follows:

$array[0][0] = Number of processes

$array[1][0] = 1st Process name

$array[1][1] = 1st Process ID (PID)

$array[2][0] = 2nd Process name

$array[2][1] = 2nd Process ID (PID)

...

$array[n][0] = nth Process name

$array[n][1] = nth Process ID (PID)

The list can be empty if $array[0][0] = 0. No @error set in this case.

So for example:

$arrProc = ProcessList()
MsgBox(0, "", $arrProc[3][1]

Will return the third processes PID.

Edited by JamesBrooks

Share this post


Link to post
Share on other sites

Thanks for your reply!

With

#include <Process.au3>
ProcessGetList()

I've this output:

: ==> Unknown function name.:
ProcessGetList()
^ ERROR

is it correct?

Thanks again!

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Sorry!

I need "handlecount", not procesid

This is an example:

wmic process get handlecount,description

Is is possible with AutoIT3?

Edited by SimV

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Sorry!

I need "handlecount", not procesid

This is an example:

wmic process get handlecount,description

Is is possible with AutoIT3?

Ahh I am sorry about that. How about this?

#include <Constants.au3>

ConsoleWrite(_HandleCount() & @CRLF)

Func _HandleCount()
    Local $sStatus = Run(@ComSpec & " /c wmic process get handlecount,description", @SystemDir, @SW_HIDE, 8)
    Local $sBuf, $sOutput
    While 1
        $sBuf &= StdoutRead($sStatus)
        If @error Then ExitLoop
    Wend
    ; Now tidy the output and get the information we want
    $sBuf = StringStripWs($sBuf, 4)
    $sBuf = StringRegExpReplace($sBuf, "[a-zA-Z_.]", "")
    $sBuf = StringStripWs($sBuf, 4)
    $aBuf = StringSplit($sBuf, " ")
    For $i = 2 To $aBuf[0] - 2 ; From the 2nd array value to the second from last to remove annoying whitespace
        $sOutput &= $aBuf[$i] & @CR
    Next
    Return $sOutput
EndFunc   ;==>_ParentDir

It returns a list (separated by @CR) with all of the handles.

Actually, that code above is useless. It's returning nothing of any use. Let's see if I can make it work like ProcessList() ;)

Edited by JamesBrooks

Share this post


Link to post
Share on other sites

Ahh I am sorry about that. How about this?

#include <Constants.au3>

ConsoleWrite(_HandleCount() & @CRLF)

Func _HandleCount()
    Local $sStatus = Run(@ComSpec & " /c wmic process get handlecount,description", @SystemDir, @SW_HIDE, 8)
    Local $sBuf, $sOutput
    While 1
        $sBuf &= StdoutRead($sStatus)
        If @error Then ExitLoop
    Wend
    ; Now tidy the output and get the information we want
    $sBuf = StringStripWs($sBuf, 4)
    $sBuf = StringRegExpReplace($sBuf, "[a-zA-Z_.]", "")
    $sBuf = StringStripWs($sBuf, 4)
    $aBuf = StringSplit($sBuf, " ")
    For $i = 2 To $aBuf[0] - 2 ; From the 2nd array value to the second from last to remove annoying whitespace
        $sOutput &= $aBuf[$i] & @CR
    Next
    Return $sOutput
EndFunc   ;==>_ParentDir

It returns a list (separated by @CR) with all of the handles.

Actually, that code above is useless. It's returning nothing of any use. Let's see if I can make it work like ProcessList() ;)

Very very good!

Thanks again!

Sim

Share this post


Link to post
Share on other sites

Very very good!

Thanks again!

Sim

Is that what you're after?

Also, if you only want to get the handlecount for one process, the WMIC query language does have a WHERE clause, so for example, you can get the HandleCount for explorer.exe like so:

ConsoleWrite(_ProcessGetHandle("explorer.exe") & @CRLF)

Func _ProcessGetHandle($ioProcName)
    Local $sStatus = Run(@ComSpec & " /c WMIC PROCESS WHERE Name='" & $ioProcName & "' GET handlecount", @SystemDir, @SW_HIDE, 8)
    Local $sBuf
    While 1
        $sBuf &= StdoutRead($sStatus)
        If @Error then ExitLoop ; We have lift off, let's go!
    WEnd
    $sBuf = StringStripCR($sBuf)
    $sBuf = StringRegExpReplace($sBuf, "HandleCount", "")
    $sBuf = StringStripWS($sBuf, 3)
    Return $sBuf
EndFunc

My regular expression is lousy, but it removes the text we don't want ;)

I also strip out the carriage returns and any whitespace which is leading or trailing.

Remember: Some processes (like cmd.exe for example) return more than one HandleCount :evil:

Share this post


Link to post
Share on other sites

Is that what you're after?

Yes!

This is perfect for me!

I need to check, kill and restart a single process with big handles leak

Also, if you only want to get the handlecount for one process, the WMIC query language does have a WHERE clause, so for example, you can get the

Very Thanks!

Sim

Share this post


Link to post
Share on other sites

Yes!

This is perfect for me!

I need to check, kill and restart a single process with big handles leak

Awesome! Here is a little script which uses the ProcessList() I showed you earlier. We loop through the processes and get the process name and its handle. I've also added some error checking to the _ProcessGetHandle function ;)

$arProc = ProcessList()

For $i = 1 to $arProc[0][0]
    $arStat = ProcessGetStats($arProc[$i][1], 0)
    If IsArray($arStat) Then
        ConsoleWrite("Process Name: " & $arProc[$i][0] & " - Handle: " & StringReplace(_ProcessGetHandle($arProc[$i][0]), @LF, " - ") & @CRLF)
    EndIf
Next

Func _ProcessGetHandle($ioProcName)
    If $ioProcName Then
        Local $sStatus = Run(@ComSpec & " /c WMIC PROCESS WHERE Name='" & $ioProcName & "' GET handlecount", @SystemDir, @SW_HIDE, 8)
        Local $sBuf
        While 1
            $sBuf &= StdoutRead($sStatus)
            If @Error then ExitLoop ; We have lift off, let's move on!
        WEnd
        $sBuf = StringStripCR($sBuf)
        $sBuf = StringRegExpReplace($sBuf, "HandleCount", "")
        $sBuf = StringStripWS($sBuf, 3)
        If StringInStr($sBuf, "No Instance(s) Available.") Then
            ; Just incase we enter a bad process name
            SetError(2)
            Return 0
        EndIf
        Return $sBuf
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc

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