Results 1 to 4 of 4

Thread: CPU Type & Speed

  1. #1
    ChrisP
    Guest

    Talking CPU Type & Speed

    Anyone know how I can return CPU type & speed in VB 6???

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    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:
    1. Option Explicit
    2. Private m_arrCPU() As String
    3. Private m_objCPUSet As SWbemObjectSet
    4. Private m_objWMINameSpace As SWbemServices
    5.  
    6.  
    7.  
    8.  
    9. Private Sub Form_Load()
    10.     Dim objCPU As SWbemObject 'WMI Object
    11.     Dim strPath As String
    12.     Dim strCaption As String
    13.     Dim lngElement As Long
    14.    
    15.     ReDim m_arrCPU(0) As String
    16.  
    17.     On Error GoTo ErrorHandler
    18.  
    19.     Set m_objWMINameSpace = GetObject("winmgmts:")
    20.     lstCPU.Clear
    21.  
    22.  
    23.     Set m_objCPUSet = m_objWMINameSpace.InstancesOf("Win32_Processor")
    24.    
    25.     strCaption = m_objCPUSet.Count & " processor"
    26.     If m_objCPUSet.Count <> 1 Then strCaption = strCaption & "s"
    27.     strCaption = strCaption & " detected on this machine"
    28.     lblTitle.Caption = strCaption
    29.                
    30.     For Each objCPU In m_objCPUSet
    31.         With objCPU
    32.             strPath = .Path_ & ""
    33.             If strPath <> "" Then
    34.                 lstCPU.AddItem .Name
    35.                 'save path to array, in case the machine has multiple CPUs,
    36.                 'each can be identified and their info loaded if needed
    37.                
    38.                 lngElement = IIf(m_arrCPU(0) = "", 0, UBound(m_arrCPU) + 1)
    39.                 ReDim Preserve m_arrCPU(lngElement) As String
    40.                 m_arrCPU(lngElement) = strPath
    41.             End If
    42.         End With
    43.     Next
    44.     If lstCPU.ListCount <> 0 Then lstCPU.ListIndex = 0
    45.      
    46. ExitProc:
    47.     Set objCPU = Nothing
    48.  
    49.     Exit Sub
    50.  
    51. ErrorHandler:
    52.     MsgBox "CPU Information could not be displayed." & vbCrLf & Err.Description, , "Error"
    53.     Resume ExitProc
    54. End Sub
    55.  
    56. Private Sub Form_Unload(Cancel As Integer)
    57.     Set m_objCPUSet = Nothing
    58.     Set m_objWMINameSpace = Nothing
    59. End Sub
    60.  
    61. Private Sub lstCPU_Click()
    62.     Dim objCPU As SWbemObject
    63.     Dim strInfo As String
    64.  
    65.     On Error Resume Next
    66.  
    67.     Set objCPU = m_objCPUSet(m_arrCPU(lstCPU.ListIndex))
    68.     With objCPU
    69.         strInfo = "Description: " & .Description & vbCrLf
    70.         strInfo = strInfo & "Processor ID: " & .ProcessorID & vbCrLf
    71.         strInfo = strInfo & "Status: " & .Status & vbCrLf
    72.         strInfo = strInfo & "Manufacturer: " & .Manufacturer & vbCrLf
    73.         strInfo = strInfo & "Availability: " & AvailabilityToString(.Availability) & vbCrLf
    74.         strInfo = strInfo & "Load Percentage: " & .LoadPercentage & vbCrLf
    75.         strInfo = strInfo & "Current Clock Speed: " & .CurrentClockSpeed & " MHz" & vbCrLf
    76.         strInfo = strInfo & "Maximum Clock Speed: " & .MaxClockSpeed & vbCrLf
    77.         strInfo = strInfo & "Level 2 Cache Size: " & .L2CacheSize & vbCrLf
    78.         strInfo = strInfo & "Level 2 Cache Speed: " & .L2CacheSpeed & vbCrLf
    79.         strInfo = strInfo & "Power Management Supported: " & .PowerManagementSupported
    80.     End With
    81.     txtCpu.Text = strInfo
    82. End Sub
    83.  
    84. Private Function AvailabilityToString(p_intCode As Integer) As String
    85.     Dim strReturn As String
    86.    
    87.     'These return p_intCodes are based on WMI SDK Documentation
    88.    
    89.     Select Case p_intCode
    90.         Case 1, 2
    91.             strReturn = "Unknown"
    92.         Case 3
    93.             strReturn = "Running/Full Power"
    94.         Case 4
    95.             strReturn = "Warning"
    96.         Case 5
    97.             strReturn = "In Test"
    98.         Case 6
    99.             strReturn = "Not Applicable"
    100.         Case 7
    101.             strReturn = "Power Off"
    102.         Case 8
    103.             strReturn = "Off Line"
    104.         Case 9
    105.             strReturn = "Off Duty"
    106.         Case 10
    107.             strReturn = "Degraded"
    108.         Case 11
    109.             strReturn = "Not Installed"
    110.         Case 12
    111.             strReturn = "Install Error"
    112.         Case 13
    113.             strReturn = "Power Save - Unknown"
    114.         Case 14
    115.             strReturn = "Power Save - Low Power Mode"
    116.         Case 15
    117.             strReturn = "Power Save - Standby"
    118.         Case 16
    119.             strReturn = "Power Cycle"
    120.         Case 17
    121.             strReturn = "Power Save - Warning"
    122.         Case Else
    123.             strReturn = "Unknown"
    124.     End Select
    125.     AvailabilityToString = strReturn
    126. End Function

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    You can also get some CPU information without using the WMI Scripting Library. Here is one example:
    VB Code:
    1. Private Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)
    2.  
    3. Private Type SYSTEM_INFO
    4.         dwOemID As Long
    5.         dwPageSize As Long
    6.         lpMinimumApplicationAddress As Long
    7.         lpMaximumApplicationAddress As Long
    8.         dwActiveProcessorMask As Long
    9.         dwNumberOrfProcessors As Long
    10.         dwProcessorType As Long
    11.         dwAllocationGranularity As Long
    12.         dwReserved As Long
    13. End Type
    14.  
    15. Private Sub Command1_Click
    16.      Dim InfoResult As SYSTEM_INFO
    17.      GetSystemInfo InfoResult
    18.      MsgBox "Your CPU type is " & InfoResult.dwProcessorType
    19. End Sub

  4. #4
    Member DoomBringer's Avatar
    Join Date
    Aug 2001
    Location
    Sweden
    Posts
    34

    Talking

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width