Well I'm not having too much luck here.
I can get the top process with Seans script but...the script itself is using a lot of cpu time and as it is supposed to be working "quietly" in the background it sort of defeats the object etc....sigh!
Code:
#NoEnv
#Persistent
#Include CoHelper.ahk
Namespace := "root\cimv2"
Class := "Win32_Process"
QLang := "WQL"
Query := "SELECT Caption, KernelModeTime, UserModeTime FROM " . Class
Ansi2Unicode(Namespace, wNamespace)
Ansi2Unicode(QLang, wQLang)
Ansi2Unicode(Query, wQuery)
CoInitialize()
GUID4String(CLSID_SWbemLocator, "{76A64158-CB41-11D1-8B02-00600806D9B6}")
GUID4String( IID_ISWbemLocator, "{76A6415B-CB41-11D1-8B02-00600806D9B6}")
ploc := CreateObject(CLSID_SWbemLocator, IID_ISWbemLocator)
DllCall(VTable(ploc, 7), "Uint", ploc, "Uint", 0, "str", wNamespace
, "Uint", 0, "Uint", 0, "Uint", 0, "Uint", 0, "int", 0, "Uint", 0, "UintP", psvc)
SetTimer TP, 50
TP:
SetTimer TP, Off
GoSub GetData ; Get first snapshot.
Sleep 10
GoSub GetData ; Get second snapshot.
Snap = %sn1%
lp = 1
GoSub Process ; Build first ss list.
Snap = %sn2%
lp = 2
GoSub Process ; Compare lists
Sort c, NR ; Bring highest cpu user to top.
ToolTip % c "Processes: " cnt
;MsgBox % c "Processes: " cnt
lp=
sn1=
sn2=
snap1=
ttl=
c=
SetTimer TP, On
Return
DllCall(VTable(penm, 2), "Unit", penm)
DllCall(VTable(pset, 2), "Unit", pset)
DllCall(VTable(psvc, 2), "Unit", psvc)
DllCall(VTable(ploc, 2), "Unit", ploc)
CoUninitialize()
Process:
StringReplace Snap, Snap, `ninstance of Win32_Process`n{,|,A
StringReplace Snap, Snap, %A_Tab%,, A
StringReplace Snap, Snap, },,A
StringReplace Snap, Snap, `;,, A
Loop Parse, Snap, |, "
{
line := A_LoopField
IfEqual line,, Continue
Loop Parse, line, `n, "
{
l := A_LoopField
IfInString l, Caption
{
StringGetPos pos, l, "
StringTrimLeft n, l, % pos+1
Continue
}
IfInString l, Handle
{
StringGetPos pos, l, "
StringTrimLeft h, l, % pos+1
Continue
}
IfInString l, KernelModeTime
{
StringGetPos pos, l, "
StringTrimLeft k, l, % pos+1
Continue
}
IfInString l, UserModeTime
{
StringGetPos pos, l, "
StringTrimLeft u, l, % pos+1
Continue
}
}
If lp = 1 ; First snapshot.
{
s1 := (k + u)/10000000 ; Add KernelModeTime and UserModeTime together.
Snap1 = %Snap1%%n%%h%-%s1%`n ; Build fist snapshot list.
Continue
}
Else { ; Second snapshot.
Sleep 10
s2 := (k + u)/10000000
Loop Parse, Snap1, `n
{
l := A_LoopField
IfInString l, System Idle Process
Continue
IfInString l, %n%%h% ; Find matching process
{
StringGetPos pos, l, -
StringTrimLeft s1, l, % pos+1 ; Get total time from 1st snap.
If s2 = %s1% ; No change.
Continue
ttl := (s2 - s1) ;/10000000 ; Process was active...
c = %c%%ttl%-%n%`n ; Build a list.
Continue
}
}
}
}
Return
GetData:
lp++ ; Which loop identifier.
DllCall(VTable(psvc,15), "Uint", psvc, "str", wQuery, "str", wQLang, "Uint", 0x30, "Uint", 0, "UintP", pset)
DllCall(VTable(pset, 7), "Uint", pset, "UintP", penm)
VarSetCapacity(varg, 8 * 2)
Loop
{
cnt := A_Index ; Count number of processes.
If DllCall(VTable(penm, 3), "Uint", penm, "Uint", 1, "Uint", &varg, "Uint", 0)
Break
pobj := DecodeInteger(&varg + 8)
DllCall(VTable(pobj,22), "Uint", pobj, "Uint", 0, "UintP", pString)
DllCall(VTable(pobj, 2), "Unit", pobj)
Unicode2Ansi(pString, sString)
SysFreeString(pString)
If lp = 1
{
sn1 = %sn1%%sString%`n
}
Else {
sn2 = %sn2%%sString%`n
}
}
Return
;Query := "SELECT Caption, KernelModeTime, UserModeTime FROM " . Class
BoBo wrote:
Good chance. If not, I think you should check out Sysinternals PSList tool meanwhile.
psList is a great little tool and the -s switch makes it run in task manager mode showing the top process.
My idea was to redirect the onscreen output to a file then have my script read then delete that file every second. The problem is that the file is locked and cannot be deleted....arrgh!
Is there an easy way to forcibly delete a locked file?
This too used more cpu time but not quite as much as the other script.
Here's a few links on this subject:
http://www.alexfedotov.com/samples/wmitop.asp
http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept05/hey0922.mspx
http://www.codeproject.com/threads/Get_CPU_Usage.asp
I have downloaded a dll that CoolMon uses to get the Top Process, is there any way that that could be used...or is that a stupid question?
Anyway I'm stuck now, any idea's?
Thanks.