【问题标题】:Get CPU Name, Architecture and Clock Speed in VB在 VB 中获取 CPU 名称、架构和时钟速度
【发布时间】:2015-11-18 20:04:29
【问题描述】:

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

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

【问题讨论】:

  • 引用的 .NET 框架类对 VB.Net 的工作方式相同。你用的是这个吗?
  • @vbnet3d 哎呀!实际上添加了错误的标签!我需要 VBA 中的答案。
  • 没问题。看这里:stackoverflow.com/questions/6866667/…
  • @vbnet3d environ() 答案似乎不起作用,例如,当我添加到我的代码 environ(15).ToString 时,它会导致 LOGONSERVER=\\MicrosoftAccount

标签: vb.net cpu cpu-speed


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我使用类似的例程。

    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
    

    【讨论】:

      【解决方案3】:

      在 Win 7 HB x64 上为我工作

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-10
        • 2021-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-11
        • 2010-11-20
        相关资源
        最近更新 更多