(VB6)有获取CPU使用率的函数吗?

注意:不是可用内存。... 注意:不是可用内存。 展开
 我来答
money0_04c351c2
2012-07-10 · TA获得超过4142个赞
知道大有可为答主
回答量:5505
采纳率:49%
帮助的人:1604万
展开全部
专用函数没有,但获取CPU占用率的方法有不少。

1、最规矩的做法是使用API,代码有点儿长,给你连接你自己去看。
http://wenku.baidu.com/view/515e1b3f87c24028915fc3e4.html

2、第二个办法是使用微软的性能统计函数库 PDH.DLL
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

3、如果系统开启了WMI服务,还可以用WMI来获取。类“Win32_Processor”中LoadPercentage属性为当前的cpu使用率
示例代码: Private Sub Timer1_Timer()
Dim WMI服务 As Object
Dim 对象 As Object
Dim 子对象 As Object
Dim 电脑名 As String
Dim 刷新 As Long
刷新 = 0
电脑名 = "." '表示本地计算机
Set WMI服务 = GetObject("winmgmts://" & 电脑名 & "/root/cimv2")
Set 对象 = WMI服务.InstancesOf("Win32_Processor")
Me.CurrentX = 0
Me.CurrentY = 0
For Each 子对象 In 对象
If 刷新 = 0 Then
刷新 = 1
Me.Cls
End If
Me.Print 子对象.Name & "[" & 子对象.CurrentClockSpeed & "Hz] 使用率:" & _
子对象.LoadPercentage & "%"
Next
End Sub
BTW: 如果你具有适当的权限,还可以通过WMI获取网络上其他电脑的数据……
ryuginka
2012-07-10 · TA获得超过956个赞
知道小有建树答主
回答量:2839
采纳率:0%
帮助的人:746万
展开全部
wmi啊。
Public Function Memory()

'====================================

'

'服务器可用户内存

'

'====================================

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

For i = 1 To 20

Set colItems = objWMIService.ExecQuery("Select * From Win32_PerfRawData_PerfOS_Memory")

For Each objItem In colItems

intValue = objItem.AvailableMbytes

Memory= "可用内存 = " & intValue & " MB"

'Memory= intValue & " MB"

Next

Next

End Function
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式