Adam Caudill

Security Leader, Researcher, Developer, Writer, & Photographer

Get CPU Speed

This post was imported from an old blog archive, and predates the creation of AdamCaudill.com.

This was created as a request on a forum I’m on, it queries the registry for the speed of the first CPU, adjusting for multiple CPU support would be simple.

Option Explicit

Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" ( _
    ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    phkResult As Long _
) As Long

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 RegCloseKey Lib "advapi32.dll" ( _
    ByVal hKey As Long _
) As Long

Private Const REG_DWORD = 4
Private Const ERROR_SUCCESS = 0&
Private Const HKEY_LOCAL_MACHINE = &H80000002

Public Function GetCPUSpeed() As Long
Dim lngResult As Long
Dim lngKeyHandle As Long
Dim lngValueType As Long
Dim lngBufferSize As Long
Dim lngBuffer As Long

    lngResult = RegOpenKey(HKEY_LOCAL_MACHINE, "HARDWARE\DESCRIPTION\System\CentralProcessor\0", lngKeyHandle)
    lngResult = RegQueryValueEx(lngKeyHandle, "~MHz", 0&, lngValueType, ByVal 0&, lngBufferSize)
    If lngValueType = REG_DWORD Then
        lngResult = RegQueryValueEx(lngKeyHandle, "~MHz", 0, 0, lngBuffer, lngBufferSize)
        If lngResult = ERROR_SUCCESS Then
            GetCPUSpeed = lngBuffer
        End If
    End If
End Function

Adam Caudill