user4838695
user4838695

Reputation:

Get CPU Name, Architecture and Clock Speed in VB

How do I find my CPU Name, Architecture and Clock Speed as a string in Visual Basic? I want it to display like it does in System Properties: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz

I need the answer in VB.net. I have found other answers in C#, C++, C and Java.

Upvotes: 3

Views: 12171

Answers (3)

omegastripes
omegastripes

Reputation: 12612

Works for me on Win 7 HB x64

MsgBox CreateObject("WScript.Shell").RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0\ProcessorNameString")

Upvotes: 1

Patrick Lepelletier
Patrick Lepelletier

Reputation: 1654

i use a similar routine for my use.

Option Explicit

Private Declare PtrSafe Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)

Type SYSTEM_INFO
   dwOemID As Long
   dwPageSize As Long
   lpMinimumApplicationAddress As Long
   lpMaximumApplicationAddress As Long
   dwActiveProcessorMask As Long
   dwNumberOfProcessors As Long
   dwProcessorType As Long
   dwAllocationGranularity As Long
   dwReserved As Long
End Type

'Sub DisplaySystemInfo()
'    Dim lpSysInfo As SYSTEM_INFO
'    GetSystemInfo lpSysInfo
'    Debug.Print "Number of processors: " & lpSysInfo.dwNumberOfProcessors
'    Debug.Print "Processor type: " & lpSysInfo.dwProcessorType
'End Sub


Function GetCPUData() As String
Dim result As String
Dim objCPUItem As Object
Dim objCPU As Object
    Err.Clear
    On Error Resume Next
    Set objCPUItem = GetObject("winmgmts:").InstancesOf("Win32_Processor")

    If Err.Number <> 0 Then
        result = "Error getting Win32_Processor " & _
        "information." & vbCrLf
    Else
        result = "Number of processors incl. Co-CPUs: " & Trim$(str$(objCPUItem.Count)) & vbCrLf & vbCrLf

        For Each objCPU In objCPUItem
            result = result & "Processor: " & objCPU.DeviceID & vbCrLf
            result = result & "Description: " & Trim$(objCPU.Name) & vbCrLf
            result = result & "Frequency (MHz): " & objCPU.MaxClockSpeed & vbCrLf
            result = result & "CPU-ID: " & objCPU.ProcessorId & vbCrLf
            result = result & vbCrLf
        Next

        Set objCPUItem = Nothing
    End If

    On Error GoTo 0
    GetCPUData = result
End Function


Function cpu3() As Long 'this sets the multi threading to max number of cpu used by excel
With Application
    .MultiThreadedCalculation.Enabled = True
    .MultiThreadedCalculation.ThreadMode = xlThreadModeAutomatic 'set to max cores
    cpu3 = .MultiThreadedCalculation.ThreadCount
    .MultiThreadedCalculation.ThreadMode = xlThreadModeManual
End With
End Function


Sub testinggg()
Dim strComputer$
Dim i&
Dim objWMIService As Object, colItems As Object, objItem As Object
strComputer = "." 'Local machine, can be adjusted to access remote    workstations
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor", , 48)
For Each objItem In colItems
    i = i + 1 '1=cpu all
    Debug.Print "-----------------------------------"
    Debug.Print "Processor " & i
    'Debug.Print "-----------------------------------"
    Debug.Print "usage: " & objItem.PercentProcessorTime
Next
Set objWMIService = Nothing
Set colItems = Nothing
Set objItem = Nothing
End Sub

Upvotes: 1

vbnet3d
vbnet3d

Reputation: 1151

VBA cannot do that directly, but you can invoke the Windows Management Interface.

Please see the information from http://www.asap-utilities.com/excel-tips-detail.php?categorie=9&m=78 (copied below)


Sub ProcessorSpeed()
' shows the processor name and speed of the computer
  Dim MyOBJ                              As Object
  Dim cpu                                As Object
  Set MyOBJ = GetObject("WinMgmts:").instancesof("Win32_Processor")
  For Each cpu In MyOBJ
        MsgBox(cpu.Name.ToString + " " + cpu.CurrentClockSpeed.ToString + " Mhz", vbInformation)
  Next
End Sub

Upvotes: 6

Related Questions