How to get current CPU and RAM usage in VB 6?

后端 未结 3 492
庸人自扰
庸人自扰 2021-01-24 17:20

how to get the CPU and memory usage in VB 6 code? Thanks!

3条回答
  •  旧时难觅i
    2021-01-24 17:43

    Determine the CPU Usage on the Current Machine

    Declarations

    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"
    

    Code

    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
    

提交回复
热议问题