Anyone know how I can return CPU type & speed in VB 6???
Printable View
Anyone know how I can return CPU type & speed in VB 6???
From Serge;
Add a ListBox (lstCPU), a textbox (txtCPU - make it multiline and high enough to see multiple lines) and a label (lblTitle) to your form. Add a reference to Microsoft WMI Scripting Library. If you dont have it installed, Download WMI SDK from MSDN (comes with 2k)
VB Code:
Option Explicit Private m_arrCPU() As String Private m_objCPUSet As SWbemObjectSet Private m_objWMINameSpace As SWbemServices Private Sub Form_Load() Dim objCPU As SWbemObject 'WMI Object Dim strPath As String Dim strCaption As String Dim lngElement As Long ReDim m_arrCPU(0) As String On Error GoTo ErrorHandler Set m_objWMINameSpace = GetObject("winmgmts:") lstCPU.Clear Set m_objCPUSet = m_objWMINameSpace.InstancesOf("Win32_Processor") strCaption = m_objCPUSet.Count & " processor" If m_objCPUSet.Count <> 1 Then strCaption = strCaption & "s" strCaption = strCaption & " detected on this machine" lblTitle.Caption = strCaption For Each objCPU In m_objCPUSet With objCPU strPath = .Path_ & "" If strPath <> "" Then lstCPU.AddItem .Name 'save path to array, in case the machine has multiple CPUs, 'each can be identified and their info loaded if needed lngElement = IIf(m_arrCPU(0) = "", 0, UBound(m_arrCPU) + 1) ReDim Preserve m_arrCPU(lngElement) As String m_arrCPU(lngElement) = strPath End If End With Next If lstCPU.ListCount <> 0 Then lstCPU.ListIndex = 0 ExitProc: Set objCPU = Nothing Exit Sub ErrorHandler: MsgBox "CPU Information could not be displayed." & vbCrLf & Err.Description, , "Error" Resume ExitProc End Sub Private Sub Form_Unload(Cancel As Integer) Set m_objCPUSet = Nothing Set m_objWMINameSpace = Nothing End Sub Private Sub lstCPU_Click() Dim objCPU As SWbemObject Dim strInfo As String On Error Resume Next Set objCPU = m_objCPUSet(m_arrCPU(lstCPU.ListIndex)) With objCPU strInfo = "Description: " & .Description & vbCrLf strInfo = strInfo & "Processor ID: " & .ProcessorID & vbCrLf strInfo = strInfo & "Status: " & .Status & vbCrLf strInfo = strInfo & "Manufacturer: " & .Manufacturer & vbCrLf strInfo = strInfo & "Availability: " & AvailabilityToString(.Availability) & vbCrLf strInfo = strInfo & "Load Percentage: " & .LoadPercentage & vbCrLf strInfo = strInfo & "Current Clock Speed: " & .CurrentClockSpeed & " MHz" & vbCrLf strInfo = strInfo & "Maximum Clock Speed: " & .MaxClockSpeed & vbCrLf strInfo = strInfo & "Level 2 Cache Size: " & .L2CacheSize & vbCrLf strInfo = strInfo & "Level 2 Cache Speed: " & .L2CacheSpeed & vbCrLf strInfo = strInfo & "Power Management Supported: " & .PowerManagementSupported End With txtCpu.Text = strInfo End Sub Private Function AvailabilityToString(p_intCode As Integer) As String Dim strReturn As String 'These return p_intCodes are based on WMI SDK Documentation Select Case p_intCode Case 1, 2 strReturn = "Unknown" Case 3 strReturn = "Running/Full Power" Case 4 strReturn = "Warning" Case 5 strReturn = "In Test" Case 6 strReturn = "Not Applicable" Case 7 strReturn = "Power Off" Case 8 strReturn = "Off Line" Case 9 strReturn = "Off Duty" Case 10 strReturn = "Degraded" Case 11 strReturn = "Not Installed" Case 12 strReturn = "Install Error" Case 13 strReturn = "Power Save - Unknown" Case 14 strReturn = "Power Save - Low Power Mode" Case 15 strReturn = "Power Save - Standby" Case 16 strReturn = "Power Cycle" Case 17 strReturn = "Power Save - Warning" Case Else strReturn = "Unknown" End Select AvailabilityToString = strReturn End Function
You can also get some CPU information without using the WMI Scripting Library. Here is one example:VB Code:
Private Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO) Private Type SYSTEM_INFO dwOemID As Long dwPageSize As Long lpMinimumApplicationAddress As Long lpMaximumApplicationAddress As Long dwActiveProcessorMask As Long dwNumberOrfProcessors As Long dwProcessorType As Long dwAllocationGranularity As Long dwReserved As Long End Type Private Sub Command1_Click Dim InfoResult As SYSTEM_INFO GetSystemInfo InfoResult MsgBox "Your CPU type is " & InfoResult.dwProcessorType End Sub
Private Const sCPURegKey = "HARDWARE\DESCRIPTION\System\CentralProcessor\0"
Private Const HKEY_LOCAL_MACHINE As Long = &H80000002
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
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 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