Using VB to detect CPU Usage


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Using VB to detect CPU Usage

  1. #1
    Join Date
    May 2005
    Posts
    1

    Unhappy Using VB to detect CPU Usage

    Hi all,

    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:
    'http://www.freevbcode.com/ShowCode.asp?ID=181

    Option Explicit

    ' 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)
    End If
    End If
    Exit Function

    errorhandler:
    On Error Resume Next
    RegCloseKey hKey
    Exit Function
    End Function

  2. #2
    Join Date
    Aug 2008
    Posts
    1
    Hi

    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.

    Thanks

  3. #3
    Join Date
    Mar 2008
    Location
    Lancashire, UK
    Posts
    156
    You could try a different method, such as this. And then either add all the percentages or just show your application's percentage

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center