vb.net 获取CPU使用率

转载 2006年06月06日 21:46:00

 

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

vb获取CPU使用率

  • 2011年03月31日 22:24
  • 4KB
  • 下载

.NET获取当前程序所在电脑的CPU和内存使用率

using System; using System.Diagnostics; using System.Text; using System.Runtime.InteropServices; n...
  • pukuimin1226
  • pukuimin1226
  • 2015年07月19日 21:19
  • 3400

获取硬盘和CPU物理序列号VB.NET源码

  • 2009年08月24日 17:40
  • 62KB
  • 下载

通过WMI获得硬盘和CPU的物理序列号(VB.net)

作者:iwebsms 获得硬盘序列号        Dim cmicWmi As New System.Management.ManagementObjectSearcher("SELECT * FR...
  • 21aspnet
  • 21aspnet
  • 2004年10月29日 21:11
  • 7455

vb c c# cpu使用率实时检测 获取CPU使用率

  • 2012年09月09日 01:16
  • 44KB
  • 下载

vb.net 获取CPU使用率

 copy一下代码到在模块下:API声明Public Declare Function PdhVbGetOneCounterPath Lib "PDH.DLL" (ByVal PathString A...
  • laidon
  • laidon
  • 2006年06月06日 21:46
  • 2617

VBS监控CPU的使用率(如占用率一直维持在80%超过30秒则运行某程序)

'---------------------------------------- On Error Resume Next dim iCpuUsePercentage'记录CPU使用率 dim...
  • a771948524
  • a771948524
  • 2013年05月05日 13:09
  • 1590

VC++获取CPU使用率

#include #include #include #define SystemBasicInformation 0#define SystemPerformanceInformation 2#de...
  • liuchen1206
  • liuchen1206
  • 2011年06月17日 09:47
  • 4585

vb获取cpu内存占用率及网卡流量监控V1

  • 2010年12月27日 01:08
  • 178KB
  • 下载

vb获取cpu内存占用率及网卡流量监控

  • 2008年09月18日 11:26
  • 178KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:vb.net 获取CPU使用率
举报原因:
原因补充:

(最多只允许输入30个字)