求一个得到CPU占用率的函数
搜索了很久,也没有找到能得到CPU占用率的函数,很多都是WMI的,看到P版说有2NtQuerySystemInformation和PDH的方法,但是都不知道怎样实现。有谁能够提供这2个方法实现的函数不胜感激啊! 这2天一直在看P版的Pdh.au3里面的例子,可是还是没有看懂,希望P版能够提供一个得到CPU使用率和内存使用率的例子。 #include <StaticConstants.au3>#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
Global $IDLETIME, $KERNELTIME, $USERTIME,$StartIdle, $StartKernel, $StartUser,$EndIdle, $EndKernel, $EndUser
$hWnd = GUICreate("CPU监控", 130, 240, -1, -1, BitOR($WS_SYSMENU, $WS_CAPTION, $WS_POPUP), 0)
$hMem_Progress = GUICtrlCreateProgress(20, 40, 29, 169, BitOR($PBS_SMOOTH, $PBS_VERTICAL))
$hCPU_Progress = GUICtrlCreateProgress(80, 40, 29, 169, BitOR($PBS_SMOOTH, $PBS_VERTICAL))
$hMem_Label = GUICtrlCreateLabel("内存", 22, 215, 37, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$hCPU_Label = GUICtrlCreateLabel("CPU", 80, 215, 35, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$hMem_Show = GUICtrlCreateLabel("", 25, 20, 37, 20)
$hCPU_Show = GUICtrlCreateLabel("", 84, 20, 35, 20)
$IDLETIME = DllStructCreate("dword;dword")
$KERNELTIME = DllStructCreate("dword;dword")
$USERTIME = DllStructCreate("dword;dword")
AdlibRegister("_TimerProc", 1000)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
Func _TimerProc()
_GetSysTime($EndIdle, $EndKernel, $EndUser)
_CPUCalc()
_GetSysTime($StartIdle, $StartKernel, $StartUser)
EndFunc ;==>_TimerProc
Func _GetSysTime(ByRef $sIdle, ByRef $sKernel, ByRef $sUser)
DllCall("kernel32.dll", "int", "GetSystemTimes", "ptr", DllStructGetPtr($IDLETIME), _
"ptr", DllStructGetPtr($KERNELTIME), _
"ptr", DllStructGetPtr($USERTIME))
$sIdle = DllStructGetData($IDLETIME, 1)
$sKernel = DllStructGetData($KERNELTIME, 1)
$sUser = DllStructGetData($USERTIME, 1)
EndFunc ;==>_GetSysTime
Func _CPUCalc()
Local $iSystemTime, $iTotal, $iCalcIdle, $iCalcKernel, $iCalcUser
$iCalcIdle = ($EndIdle - $StartIdle)
$iCalcKernel = ($EndKernel - $StartKernel)
$iCalcUser = ($EndUser - $StartUser)
$iSystemTime = ($iCalcKernel + $iCalcUser)
$iTotal = Int(($iSystemTime - $iCalcIdle) * (100 / $iSystemTime))
Local $CPU = $iTotal
Local $tArr = MemGetStats()
Local $RAM = $tArr
If $CPU & "%" <> GUICtrlRead($hCPU_Show) And ($CPU >= 0 And $CPU <= 100) Then
GUICtrlSetData($hCPU_Progress, $CPU)
GUICtrlSetData($hCPU_Show, $CPU & "%")
EndIf
If $RAM & "%" <> GUICtrlRead($hMem_Show) And ($RAM >= 0 And $RAM <= 100) Then
GUICtrlSetData($hMem_Progress, $RAM)
GUICtrlSetData($hMem_Show, $RAM & "%")
EndIf
EndFunc ;==>_CPUCalc 回复 3# 鸟人
多谢了,可是我没有看懂,我太菜了,呵呵。
能不够帮我改改,比如当前我看见的任务管理器里CPU使用时20%,我调用_CPUCalc()函数就能得到返回值20或者20%,非常感谢帮组啊。
页:
[1]