#console

Declare Function PdhOpenQuery Lib "pdh" Alias "PdhVbOpenQuery" ( _
ByRef QueryHandle As Long) As Long

Declare Function PdhAddCounter Lib "pdh" Alias "PdhVbAddCounter" ( _
ByVal QueryHandle As Long, _
ByVal CounterPath As String, _
ByRef CounterHandle As Long) As Long

Declare Function PdhCollectQueryData Lib "pdh" ( _
ByVal QueryHandle As Long) As Long

Declare Function PdhGetDoubleCounterValue Lib "pdh" _
Alias "PdhVbGetDoubleCounterValue" ( _
ByVal CounterHandle As Long, _
ByRef CounterStatus As Long) As Double

Declare Function PdhCloseQuery Lib "pdh" ( _
ByVal QueryHandle As Long) As Long

'CPU使用率を返す(%)
Function GetCPUutilization(wait As Long) As Long
Dim hQuery As Long
Dim hCounter As Long
Dim CounterStatus As Long

PdhOpenQuery(hQuery)
PdhAddCounter( hQuery, "\Processor(_Total)\% Processor Time", hCounter)
PdhCollectQueryData( hQuery)
Sleep(wait)
PdhCollectQueryData( hQuery)
GetCPUutilization = PdhGetDoubleCounterValue(hCounter, CounterStatus)
PdhCloseQuery( hQuery)
End Function

Dim cnt As Long
cnt = 100
While cnt
Print GetCPUutilization(500)
cnt = cnt -1
Wend

End