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
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
Hi,
From: Pe?, Botp <botp@delmonte-phil.com>
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: windows task manager listing
Date: Sat, 3 Jun 2006 14:29:53 +0900Hi 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..
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 Heesob