在 VB 中获取 CPU 名称、体系结构和时钟速度
Get CPU Name, Architecture and Clock Speed in VB
如何在 Visual Basic 中找到 CPU
Name
、Architecture
和 Clock Speed
作为字符串?我希望它像 System Properties
中那样显示:Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
我需要 VB.net
中的答案。我 在 C#
、C++
、C
和 Java
.
中找到了其他答案
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
适用于 Win 7 HB x64
MsgBox CreateObject("WScript.Shell").RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor[=10=]\ProcessorNameString")
我使用类似的例程。
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
如何在 Visual Basic 中找到 CPU
Name
、Architecture
和 Clock Speed
作为字符串?我希望它像 System Properties
中那样显示:Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
我需要 VB.net
中的答案。我 在 C#
、C++
、C
和 Java
.
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
适用于 Win 7 HB x64
MsgBox CreateObject("WScript.Shell").RegRead("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor[=10=]\ProcessorNameString")
我使用类似的例程。
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