vb.net 在 VB 中获取 CPU 名称、架构和时钟速度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33789321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:34:45  来源:igfitidea点击:

Get CPU Name, Architecture and Clock Speed in VB

vb.netcpucpu-speed

提问by vbnet3d

How do I find my CPUName, Architectureand Clock Speedas 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

如何找到我CPUNameArchitectureClock Speed为Visual Basic中的字符串?我希望它像这样显示System PropertiesIntel(R) Core(TM) i5-3210M CPU @ 2.50GHz

I need the answer in VB.net. I havefound other answers in C#, C++, Cand Java.

我需要答案在VB.net. 我已经发现了在其他的答案C#C++CJava

回答by vbnet3d

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

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

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

请参阅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

回答by omegastripes

Works for me on Win 7 HB x64

在 Win 7 HB x64 上对我有用

MsgBox CreateObject("WScript.Shell").RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor
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
\ProcessorNameString")

回答by Patrick Lepelletier

i use a similar routine for my use.

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

##代码##