Using VB to detect CPU Usage
I need to use VB 6.0 coding to detect the current CPU usage on a Windows 2000 Server machine. I searched on the Net high and low and only came up with this (see end of message). However, it didin't work (RegOpenKeyEx returned value 6 instead of 0 which indicates success). MS website said RegOpenKeyEx can only have HKEY_DYN_DATA as the first parameter for ME/98/95 (http://msdn.microsoft.com/library/de...gopenkeyex.asp).
Question: how can I change the below code so that it works on the system I have? Better yet, anyone has some existing code?
Thanks for your help.
'All of the below code comes from:
' Note that if you declare the lpData parameter as String, you
' must pass it By Value.
Private Declare Function RegQueryValueEx Lib "advapi32.dll" _
Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName _
As String, ByVal lpReserved As Long, lpType As Long, lpData As _
Any, lpcbData As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal ulOptions As Long, ByVal samDesired As Long, _
phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Private Const KEY_QUERY_VALUE = &H1
Private Const HKEY_DYN_DATA As Long = &H80000006
Private Const STAT_DATA = "PerfStats\StatData"
Private Const CPU_USE = "KERNEL\CPUUsage"
Public Function CPU_Usage_Percent() As Long
On Error GoTo errorhandler:
Dim lResult As Long
Dim lData As Long
Dim lType As Long
Dim hKey As Long
lResult = RegOpenKeyEx(HKEY_DYN_DATA, STAT_DATA, _
0, KEY_QUERY_VALUE, hKey)
If lResult = 0 Then
lResult = RegQueryValueEx(hKey, CPU_USE, 0, _
lType, lData, 4)
If lResult = 0 Then
CPU_Usage_Percent = lData
lResult = RegCloseKey(hKey)
On Error Resume Next
I'm having the same problem. Does nayone know what the fix is? I noticed that if I change the HKEY_DYN_DATA to 5, RegOpenKeyEx returns 2 instead of 6.
You could try a different method, such as this. And then either add all the percentages or just show your application's percentage
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center