关闭

vb.net 获取CPU使用率

标签: vb.netintegerfunctionstringstructureallocation
2366人阅读 评论(1) 收藏 举报

 

copy一下代码到在模块下:

'API声明
Public Declare Function PdhVbGetOneCounterPath Lib "PDH.DLL" (ByVal PathString As String, ByVal PathLength As Integer, ByVal DetailLevel As Integer, ByVal CaptionString As String) As Integer
Public Declare Function PdhVbCreateCounterPathList Lib "PDH.DLL" (ByVal PERF_DETAIL As Integer, ByVal CaptionString As String) As Integer
Public Declare Function PdhVbGetCounterPathFromList Lib "PDH.DLL" (ByVal Index As Integer, ByVal Buffer As String, ByVal BufferLength As Integer) As Integer
Public Declare Function PdhOpenQuery Lib "PDH.DLL" (ByVal Reserved As Integer, ByVal dwUserData As Integer, ByRef hQuery As Integer) As PDH_STATUS
Public Declare Function PdhCloseQuery Lib "PDH.DLL" (ByVal hQuery As Integer) As PDH_STATUS
Public Declare Function PdhVbAddCounter Lib "PDH.DLL" (ByVal QueryHandle As Integer, ByVal CounterPath As String, ByRef CounterHandle As Integer) As PDH_STATUS
Public Declare Function PdhCollectQueryData Lib "PDH.DLL" (ByVal QueryHandle As Integer) As PDH_STATUS
Public Declare Function PdhVbIsGoodStatus Lib "PDH.DLL" (ByVal StatusValue As Integer) As Integer
Public Declare Function PdhVbGetDoubleCounterValue Lib "PDH.DLL" (ByVal CounterHandle As Integer, ByRef CounterStatus As Integer) As Double

' 枚举常数
Enum PERF_DETAIL
PERF_DETAIL_NOVICE = 100
PERF_DETAIL_ADVANCED = 200
PERF_DETAIL_EXPERT = 300
PERF_DETAIL_WIZARD = 400
End Enum

Enum PDH_STATUS
PDH_CSTATUS_VALID_DATA = &H0S
PDH_CSTATUS_NEW_DATA = &H1S
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

Public Const ERROR_SUCCESS As Short = 0

Public Structure CounterInfo
Dim hCounter As Integer
Dim strName As String
End Structure


在窗体的load事件里面添加一下语句:
Dim pdhStatus As PDH_STATUS
  
pdhStatus = PdhOpenQuery(0, 1, hQuery)
If pdhStatus <> ERROR_SUCCESS Then
MsgBox("OpenQuery failed")
End
End If

在窗体的closed事件里面添加以下代码
PdhCloseQuery(hQuery)

' 显示利用率信息
 Private Sub UpdateValues()
  Dim dblCounterValue As Double
  Dim pdhStatus As Integer
  Dim strInfo As String
  Dim i As Integer
  
  PdhCollectQueryData(hQuery)
  
  i = 0
  dblCounterValue = PdhVbGetDoubleCounterValue(Counters(i).hCounter, pdhStatus)
  
  ' 将CPU利用率信息显示在标签中
  If (pdhStatus = PDH_STATUS.PDH_CSTATUS_VALID_DATA) or (pdhStatus = PDH_STATUS.PDH_CSTATUS_NEW_DATA) Then
strInfo = "CPU 使用率: " & VB6.Format(dblCounterValue, "0.00")
   pb1.Value = dblCounterValue
Me.Text = VB6.Format(dblCounterValue, "0") & "% - CPU 当前状态"
  End If
  
  Label1.Text = strInfo
 End Sub
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13254次
    • 积分:227
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:3篇
    • 译文:0篇
    • 评论:3条
    最新评论