# Windows task manager listing

**URL:** <https://www.ruby-forum.com/t/windows-task-manager-listing/62359>\
**Category:** Ruby\
**Created:** [June 3, 2006, 7:30am UTC](https://www.ruby-forum.com/t/windows-task-manager-listing/62359 "2006-06-03T07:30:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [June 3, 2006, 7:30am UTC](https://www.ruby-forum.com/t/windows-task-manager-listing/62359/1 "2006-06-03T07:30:21Z")

</div>

Hi Win32,

do we have a util that lists tasks like windows task manager? And, if  
yes, allows one to kill or set priority on selected tasks…

thanks for win32 utils.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [June 3, 2006, 9:13am UTC](https://www.ruby-forum.com/t/windows-task-manager-listing/62359/2 "2006-06-03T09:13:07Z")

</div>

Hi,

> 

Not yet.

But you can do it like this:

require ‘Win32API’

RegCloseKey = Win32API.new(“advapi32”,“RegCloseKey”,[‘L’],‘L’)  
RegOpenKeyEx =  
Win32API.new(“advapi32”,“RegOpenKeyEx”,[‘L’,‘P’,‘L’,‘L’,‘P’],‘L’)  
RegQueryValueEx =  
Win32API.new(“advapi32”,“RegQueryValueEx”,[‘L’,‘P’,‘L’,‘P’,‘P’,‘P’],‘L’)  
WideCharToMultiByte = Win32API.new(‘kernel32’, ‘WideCharToMultiByte’,  
‘ILPIPIPP’, ‘I’)

INITIAL\_SIZE = 51200  
EXTEND\_SIZE = 25600  
REGKEY\_PERF = “SOFTWARE\Microsoft\Windows  
NT\CurrentVersion\Perflib\009”  
REGSUBKEY\_COUNTERS = “Counters”  
KEY\_READ = 0x20019;  
ERROR\_MORE\_DATA = 234  
HKEY\_LOCAL\_MACHINE = 0x80000002  
HKEY\_PERFORMANCE\_DATA = 0x80000004

def get\_task\_list()  
result = Hash.new  
hKeyNames = “\0”\*4  
rc =  
RegOpenKeyEx.Call(HKEY\_LOCAL\_MACHINE,REGKEY\_PERF,0,KEY\_READ,hKeyNames)  
hKeyNames = hKeyNames.unpack(‘L’).first  
dwSize = “\0”\*4  
rc = RegQueryValueEx.Call( hKeyNames,REGSUBKEY\_COUNTERS,0,0,0,dwSize)  
dwSize2 = dwSize.unpack(‘L’).first  
buf = “\0” \* dwSize2  
rc = RegQueryValueEx.Call( hKeyNames,REGSUBKEY\_COUNTERS,0,0,buf,dwSize)

buf2 = Hash[\*buf.split("\0")]

proidx = buf2.index(“Process”).to\_i  
pididx = buf2.index(“ID Process”).to\_i

buf = “\0” \* INITIAL\_SIZE  
szSubKey = proidx.to\_s  
dwSize = [INITIAL\_SIZE]

while true  
dwSize2 = dwSize.pack(‘L’)  
rc = RegQueryValueEx.Call(  
HKEY\_PERFORMANCE\_DATA,szSubKey,0,0,buf,dwSize2)  
break if rc==0  
if rc == ERROR\_MORE\_DATA  
dwSize[0] += EXTEND\_SIZE  
buf = “\0” \* dwSize.first  
end  
end

pObj = buf[buf[24,4].unpack(‘L’).first … -1]

pCounterDef = pObj[pObj[8,4].unpack(‘L’).first … -1]  
numCounters = pObj[32,4].unpack(‘L’).first

for i in 0 … numCounters  
counterNameTitleIndex = pCounterDef[4,4].unpack(‘L’).first  
if counterNameTitleIndex == pididx  
pidcounter = pCounterDef[36,4].unpack(‘L’).first  
end  
pCounterDef = pCounterDef[40…-1]  
end

dwNumTasks = pObj[40,4].unpack(‘L’).first  
pInst = pObj[pObj[4,4].unpack(‘L’).first … -1]

for i in 0 … dwNumTasks  
pCounter = pInst[pInst[0,4].unpack(‘L’).first … -1]  
buf = 0.chr \* 260  
int = WideCharToMultiByte.call(0, 0,  
pInst[pInst[16,4].unpack(‘L’).first ,  
pInst[20,4].unpack(‘L’).first], -1, buf, buf.size, 0, 0)  
pid = pCounter[pidcounter,4].unpack(‘L’).first  
result[pid] = buf[0, int].strip if pid\>0  
pInst = pCounter[pCounter[0,4].unpack(‘L’).first … -1]  
end  
RegCloseKey.Call( hKeyNames )  
RegCloseKey.Call( HKEY\_PERFORMANCE\_DATA )  
result  
end

get\_task\_list.each {|pid,name|  
Process.kill(9,pid) if name==‘iexplore’  
}

Setting priority of a process is possible using SetPriorityClass API.

Regards,

Park H.
