SimV Posted January 4, 2010 Share Posted January 4, 2010 Hello! Is it possible to get handles of a single running process? Thanks Link to comment Share on other sites More sharing options...
SimV Posted January 4, 2010 Author Share Posted January 4, 2010 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 [..] Link to comment Share on other sites More sharing options...
James Posted January 4, 2010 Share Posted January 4, 2010 (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 January 4, 2010 by JamesBrooks Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
SimV Posted January 4, 2010 Author Share Posted January 4, 2010 Thanks for your reply! With #include <Process.au3> ProcessGetList() I've this output: : ==> Unknown function name.: ProcessGetList() ^ ERROR is it correct? Thanks again! Link to comment Share on other sites More sharing options...
James Posted January 4, 2010 Share Posted January 4, 2010 Woops, I mean ProcessList(). You don't need the Process.au3 include. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
SimV Posted January 4, 2010 Author Share Posted January 4, 2010 (edited) Sorry!I need "handlecount", not procesidThis is an example:wmic process get handlecount,descriptionIs is possible with AutoIT3? Edited January 4, 2010 by SimV Link to comment Share on other sites More sharing options...
James Posted January 4, 2010 Share Posted January 4, 2010 (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 January 4, 2010 by JamesBrooks Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
SimV Posted January 4, 2010 Author Share Posted January 4, 2010 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 Link to comment Share on other sites More sharing options...
James Posted January 4, 2010 Share Posted January 4, 2010 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 Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
SimV Posted January 4, 2010 Author Share Posted January 4, 2010 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 leakAlso, 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 Link to comment Share on other sites More sharing options...
James Posted January 4, 2010 Share Posted January 4, 2010 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 Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now