CPUの稼働率を取得する(Win9x専用)

Win9xではCPUの使用率はレジストリを見ればわかる。
ただし、あるきっかけがないとこのレジストリは更新がかからないので注意。
コメントは少ないけどやってることは簡単なんで頑張って解読してください。

            '[basCPUUsage.bas]
            '   CPU使用率  取得モジュール
            '                       Ver1.0.1
            '                                  Programinged By Uz.
            '
            Option Explicit

            Public 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

            Public 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

            Public Const HKEY_DYN_DATA = &H80000006

            Public Function GetCPUUsage() As Long
                '失敗のときは   -1 を返す
                Dim hKey As Long
                
                Dim lpSubKey As String
                Dim ulOptions As Long
                Dim samDesired As Long
                Dim phkResult As Long
                
                Dim lpValueName As String
                Dim lpReserved As Long
                Dim lpType As Long
                Dim lpData(4) As Byte
                Dim lpcbData As Long
                
                hKey = HKEY_DYN_DATA
                lpSubKey = "PerfStats\StartStat"
                ulOptions = 0
                samDesired = 0
                phkResult = 0
                
                Call RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, phkResult)

                hKey = phkResult
                lpValueName = "KERNEL\CPUUsage"
                lpReserved = 0
                lpType = VarType(lpData)
                lpcbData = 4
                
                If RegQueryValueEx(hKey, lpValueName, lpReserved, lpType, lpData(0), lpcbData) <> 0 Then
                    GetCPUUsage = -1
                    Exit Function
                End If
                
                hKey = HKEY_DYN_DATA
                lpSubKey = "PerfStats\StatData"
                ulOptions = 0
                samDesired = 0
                phkResult = 0
                
                Call RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, phkResult)
                
                hKey = phkResult
                lpValueName = "KERNEL\CPUUsage"
                lpReserved = 0
                lpType = VarType(lpData)
                lpcbData = 4
                
                If RegQueryValueEx(hKey, lpValueName, lpReserved, lpType, lpData(0), lpcbData) = 0 Then
                    GetCPUUsage = lpData(0)
                Else
                    GetCPUUsage = -1
                End If
            End Function


            '[frmMain]
            '   フォーム側はこれだけ(^^)
            '
            Option Explicit

            'タイマー
            Private Sub timTimer_Timer()
                'ラベル更新
                lblCPU.Caption = GetCPUUsage & "%"
            End Sub



サンプル cpuusage.lzh