Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CPU Speed ? 1

Status
Not open for further replies.

TomB

Programmer
Feb 23, 2001
27
DE
Hi,

is it possible to get the CPU Speed in VB ? How ?

Thanks,
Tom
 
Here you go.

You’ll need to add these Constants

Private Const sCPURegKey _ = "HARDWARE\DESCRIPTION\System\CentralProcessor\0"
Private Const HKEY_LOCAL_MACHINE As Long = &H80000002


And Declare

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


This is the Function to use.

Private Function GetCPUSpeed() As Long

Dim HKey As Long
Dim cpuSpeed As Long

'Open CPU key
Call RegOpenKey(HKEY_LOCAL_MACHINE, sCPURegKey, HKey)

'and retrieve the value
Call RegQueryValueEx(HKey, "~MHz", 0, 0, cpuSpeed, 4)
Call RegCloseKey(HKey)

GetCPUSpeed = cpuSpeed
End Function


Finally you can return the value to a control, in this example I choose a button(Command1) event and a textbox named “txtCPUSpeed.txt”.

Private Sub Command1_Click()
txtCPUSpeed = " " & GetCPUSpeed() & " MHz"
End Sub


Thats it!
 
Thanks, but that works only with Win NT. Win98 does not provide this key in the registry. Any ideas for a solution for all Windows Systems ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top