VB6 獲取CPUID的幾种方法

第一种:API:

Private Type OSVERSIONINFO

dwOSVersionInfoSize As Long

dwMajorVersion As Long

dwMinorVersion As Long

dwBuildNumber As Long

dwPlatformId As Long

szCSDVersion As String * 128 ' Maintenance string for PSS usage

End Type

Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function GetCPUid() As String

On Error GoTo doError

Dim len5 As Long, aa As Long

Dim cmprName As String

Dim Computer As String

Dim CPUs As Object, MyCpu As Object

Dim osver As OSVERSIONINFO

Dim SerialNo As String

'取得Computer Name

cmprName = String(255, 0)

len5 = 256

aa = GetComputerName(cmprName, len5)

cmprName = Left(cmprName, InStr(1, cmprName, Chr(0)) - 1)

Computer = cmprName '取得CPU端口号

Set CPUs = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2").ExecQuery("select * from Win32_Processor")

For Each MyCpu In CPUs

SerialNo = MyCpu.ProcessorId

Exit For

Next

GetCPUid = SerialNo

Exit Function

doError:

GetCPUid = ""

End Function

第二种:使用类:

'Microsoft WMI Scripting V1.1 Library的引入

Public Function GetProcessorID() As String

Dim a As SWbemServices

Dim b As SWbemObjectSet

Dim c As SWbemObject

Dim d As SWbemPropertySet

Dim e As SWbemProperty

Dim str As String

Set a = GetObject("winmgmts:")

Set b = a.InstancesOf("Win32_Processor")

For Each c In b

With c

If .Properties_.Count > 0 Then

Set d = .Properties_

For Each e In d

If UCase(e.Name) = UCase("ProcessorId") Then

str = e.Value

End If

Next

End If

End With

Next

GetProcessorID = str

End Function