Code: Select all
#COUNTERPERF_PROCESSOR = 238
#COUNTERPERF_PERCENTPROCESSORTIME = 6
Global NumCores.w
Global hQuery.l
Structure PDH_FMT_COUNTERVALUE
CStatus.l; As Long
; case Cardinal of
; 0: (longValue: Integer);
; 1: (doubleValue: Double);
; 2: (largeValue: Int64);
; 3: (AnsiStringValue: PChar);
; 4: (WideStringValue: PWideChar);
padding.l; As Long
ulValueLow.l; As Long
ulValueHigh.l; As Long
EndStructure
Global Dim cpuCounters.l(1)
Global Dim dCPU.d(1)
#PDH_CSTATUS_VALID_DATA = 0
#PDH_CSTATUS_NEW_DATA = 1
Declare.s GetCPUCounter(strInstance.s)
Procedure Close_CPU_Usage()
PdhCloseQuery_(hQuery) ; close cpu usage
EndProcedure
Procedure.l InitializeCPU()
pdhStatus.l: SysInfo.SYSTEM_INFO: CPU_Obj.s
hCounter.l: i.l
; get # of cpus
GetSystemInfo_(@SysInfo)
NumCores = SysInfo\dwNumberOfProcessors - 1
ReDim dCPU.d(NumCores)
ReDim cpuCounters.l(NumCores)
pdhStatus = PdhOpenQuery_(0, 1, @hQuery)
If pdhStatus = 0
For i = 0 To NumCores
CPU_Obj = GetCPUCounter(Trim(Str(i)))
If PdhValidatePath_(CPU_Obj) <> #ERROR_SUCCESS
MessageRequester("Error","Unable To initialize CPU usage.")
EndIf
pdhStatus = PdhAddCounter_(hQuery, CPU_Obj, 0, @hCounter)
If pdhStatus = #PDH_CSTATUS_VALID_DATA: cpuCounters(i) = hCounter: EndIf
Next i
If hCounter <> 0
ProcedureReturn 1
EndIf
EndIf
MessageRequester("Error","Unable To initialize CPU usage.")
EndProcedure
Procedure Update_Cpu_Usage()
; get usage per core
hIcon.l: pdhStatus.l
i.l
lpValue.PDH_FMT_COUNTERVALUE
PdhCollectQueryData_(hQuery)
For i = 0 To NumCores
PdhGetFormattedCounterValue_(cpuCounters(i), $200, @dwType.l, @lpValue)
CopyMemory(@lpValue\ulValueLow, @dCPU(i),8)
Next
EndProcedure
Procedure.s GetCPUCounter(strInstance.s)
; get Object & Counter names for CPU Usage
; Different languages of windows use different names so we use a look-up!
If OpenLibrary(0, "pdh.dll")
*F = GetFunction(0, "PdhLookupPerfNameByIndexA")
If *F
NameLen.l: ObjectName.s: CounterName.s
NameLen = #MAX_PATH
ObjectName = Space(NameLen)
i = CallFunctionFast(*F,#Null, #COUNTERPERF_PROCESSOR, @ObjectName, @NameLen)
NameLen = #MAX_PATH
CounterName = Space(NameLen)
i = CallFunctionFast(*F,#Null, #COUNTERPERF_PERCENTPROCESSORTIME, @CounterName, @NameLen)
Debug "\" + ObjectName + "(" + strInstance + ")\" + CounterName
ProcedureReturn "\" + ObjectName + "(" + strInstance + ")\" + CounterName
EndIf
CloseLibrary(0)
EndIf
EndProcedure
initializecpu()
For i=1 To 40
update_cpu_usage()
For ii=0 To NumCores
tmp.s + "Core #" + Str(ii) + " usage = " + Str(dCPU(ii)) + "% "
Next
Debug tmp
tmp=""
Delay(1000)
Next
close_cpu_usage()