获取VB中的CPU名称,体系结构和时钟速度

时间:2015-11-18 20:04:29

标签: vb.net cpu cpu-speed

如何在Visual Basic中找到CPU NameArchitectureClock Speed作为字符串?我希望它在System Properties中显示:Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz

我需要VB.net中的答案。我 C#C++CJava找到了其他答案。

3 个答案:

答案 0 :(得分:4)

VBA不能直接执行此操作,但您可以调用Windows管理界面。

请参阅http://www.asap-utilities.com/excel-tips-detail.php?categorie=9&m=78(下方复制)

中的信息
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

答案 1 :(得分:1)

在Win 7 HB x64上为我工作

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

答案 2 :(得分:1)

我使用类似的例程供我使用。

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