求VB代码:得到CPU占用率
求一个可以获得CPU当前占用率的代码,并且能像资源管理器中那样在PICTURE控件中画出CPU占用率的变化折线图。...
求一个可以获得CPU当前占用率的代码,并且能像资源管理器中那样在PICTURE控件中画出CPU占用率的变化折线图。
展开
1个回答
展开全部
Private Type CounterInfo
hCounter As Long
strName As String
End Type
Private Declare Function PdhCollectQueryData Lib "PDH.DLL" (ByVal QueryHandle As Long) As PDH_STATUS
Private Declare Function PdhVbGetDoubleCounterValue Lib "PDH.DLL" (ByVal CounterHandle As Long, ByRef CounterStatus As Long) As Double
Private Declare Function PdhOpenQuery Lib "PDH.DLL" (ByVal Reserved As Long, ByVal dwUserData As Long, ByRef hQuery As Long) As PDH_STATUS
Private Declare Function PdhVbAddCounter Lib "PDH.DLL" (ByVal QueryHandle As Long, ByVal CounterPath As String, ByRef CounterHandle As Long) As PDH_STATUS
Dim Counters(0 To 99) As CounterInfo
Dim hQuery As Long
Enum PDH_STATUS
PDH_CSTATUS_VALID_DATA = &H0
PDH_CSTATUS_NEW_DATA = &H1
PDH_CSTATUS_NO_MACHINE = &H800007D0
PDH_CSTATUS_NO_INSTANCE = &H800007D1
PDH_MORE_DATA = &H800007D2
PDH_CSTATUS_ITEM_NOT_VALIDATED = &H800007D3
PDH_RETRY = &H800007D4
PDH_NO_DATA = &H800007D5
PDH_CALC_NEGATIVE_DENOMINATOR = &H800007D6
PDH_CALC_NEGATIVE_TIMEBASE = &H800007D7
PDH_CALC_NEGATIVE_VALUE = &H800007D8
PDH_DIALOG_CANCELLED = &H800007D9
PDH_CSTATUS_NO_OBJECT = &HC0000BB8
PDH_CSTATUS_NO_COUNTER = &HC0000BB9
PDH_CSTATUS_INVALID_DATA = &HC0000BBA
PDH_MEMORY_ALLOCATION_FAILURE = &HC0000BBB
PDH_INVALID_HANDLE = &HC0000BBC
PDH_INVALID_ARGUMENT = &HC0000BBD
PDH_FUNCTION_NOT_FOUND = &HC0000BBE
PDH_CSTATUS_NO_COUNTERNAME = &HC0000BBF
PDH_CSTATUS_BAD_COUNTERNAME = &HC0000BC0
PDH_INVALID_BUFFER = &HC0000BC1
PDH_INSUFFICIENT_BUFFER = &HC0000BC2
PDH_CANNOT_CONNECT_MACHINE = &HC0000BC3
PDH_INVALID_PATH = &HC0000BC4
PDH_INVALID_INSTANCE = &HC0000BC5
PDH_INVALID_DATA = &HC0000BC6
PDH_NO_DIALOG_DATA = &HC0000BC7
PDH_CANNOT_READ_NAME_STRINGS = &HC0000BC8
End Enum
Private Sub UpdateValues()
Dim dblCounterValue As Double
Dim pdhStatus As Long
Dim strInfo As String
Dim i As Long
PdhCollectQueryData (hQuery)
dblCounterValue = _
PdhVbGetDoubleCounterValue(Counters(i).hCounter, pdhStatus)
If (pdhStatus = PDH_CSTATUS_VALID_DATA) _
Or (pdhStatus = PDH_CSTATUS_NEW_DATA) Then
MsgBox "CPU使用情况:" & Format$(dblCounterValue, "0.0") & "%"
End If
End Sub
Private Sub AddCounter(strCounterName As String, hQuery As Long)
Dim pdhStatus As PDH_STATUS
Dim hCounter As Long
pdhStatus = PdhVbAddCounter(hQuery, strCounterName, hCounter)
Counters(currentCounterIdx).hCounter = hCounter
currentCounterIdx = currentCounterIdx + 1
End Sub
Private Sub Form_Load()
Dim pdhStatus As PDH_STATUS
pdhStatus = PdhOpenQuery(0, 1, hQuery)
AddCounter "\Processor(0)\% Processor Time", hQuery
UpdateValues
End Sub
截下来的代码
hCounter As Long
strName As String
End Type
Private Declare Function PdhCollectQueryData Lib "PDH.DLL" (ByVal QueryHandle As Long) As PDH_STATUS
Private Declare Function PdhVbGetDoubleCounterValue Lib "PDH.DLL" (ByVal CounterHandle As Long, ByRef CounterStatus As Long) As Double
Private Declare Function PdhOpenQuery Lib "PDH.DLL" (ByVal Reserved As Long, ByVal dwUserData As Long, ByRef hQuery As Long) As PDH_STATUS
Private Declare Function PdhVbAddCounter Lib "PDH.DLL" (ByVal QueryHandle As Long, ByVal CounterPath As String, ByRef CounterHandle As Long) As PDH_STATUS
Dim Counters(0 To 99) As CounterInfo
Dim hQuery As Long
Enum PDH_STATUS
PDH_CSTATUS_VALID_DATA = &H0
PDH_CSTATUS_NEW_DATA = &H1
PDH_CSTATUS_NO_MACHINE = &H800007D0
PDH_CSTATUS_NO_INSTANCE = &H800007D1
PDH_MORE_DATA = &H800007D2
PDH_CSTATUS_ITEM_NOT_VALIDATED = &H800007D3
PDH_RETRY = &H800007D4
PDH_NO_DATA = &H800007D5
PDH_CALC_NEGATIVE_DENOMINATOR = &H800007D6
PDH_CALC_NEGATIVE_TIMEBASE = &H800007D7
PDH_CALC_NEGATIVE_VALUE = &H800007D8
PDH_DIALOG_CANCELLED = &H800007D9
PDH_CSTATUS_NO_OBJECT = &HC0000BB8
PDH_CSTATUS_NO_COUNTER = &HC0000BB9
PDH_CSTATUS_INVALID_DATA = &HC0000BBA
PDH_MEMORY_ALLOCATION_FAILURE = &HC0000BBB
PDH_INVALID_HANDLE = &HC0000BBC
PDH_INVALID_ARGUMENT = &HC0000BBD
PDH_FUNCTION_NOT_FOUND = &HC0000BBE
PDH_CSTATUS_NO_COUNTERNAME = &HC0000BBF
PDH_CSTATUS_BAD_COUNTERNAME = &HC0000BC0
PDH_INVALID_BUFFER = &HC0000BC1
PDH_INSUFFICIENT_BUFFER = &HC0000BC2
PDH_CANNOT_CONNECT_MACHINE = &HC0000BC3
PDH_INVALID_PATH = &HC0000BC4
PDH_INVALID_INSTANCE = &HC0000BC5
PDH_INVALID_DATA = &HC0000BC6
PDH_NO_DIALOG_DATA = &HC0000BC7
PDH_CANNOT_READ_NAME_STRINGS = &HC0000BC8
End Enum
Private Sub UpdateValues()
Dim dblCounterValue As Double
Dim pdhStatus As Long
Dim strInfo As String
Dim i As Long
PdhCollectQueryData (hQuery)
dblCounterValue = _
PdhVbGetDoubleCounterValue(Counters(i).hCounter, pdhStatus)
If (pdhStatus = PDH_CSTATUS_VALID_DATA) _
Or (pdhStatus = PDH_CSTATUS_NEW_DATA) Then
MsgBox "CPU使用情况:" & Format$(dblCounterValue, "0.0") & "%"
End If
End Sub
Private Sub AddCounter(strCounterName As String, hQuery As Long)
Dim pdhStatus As PDH_STATUS
Dim hCounter As Long
pdhStatus = PdhVbAddCounter(hQuery, strCounterName, hCounter)
Counters(currentCounterIdx).hCounter = hCounter
currentCounterIdx = currentCounterIdx + 1
End Sub
Private Sub Form_Load()
Dim pdhStatus As PDH_STATUS
pdhStatus = PdhOpenQuery(0, 1, hQuery)
AddCounter "\Processor(0)\% Processor Time", hQuery
UpdateValues
End Sub
截下来的代码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询