如何用VB读取计算机主板或CPU的机器码

如何用VB读取计算机主板或CPU的机器码,第1张

仅仅几行代码实现取得本机CPU序列号,可以使用此序列号实现程序的注册功能。个人感觉使用CPU序列号注册比使用硬盘序列号更为适合。

'引用 Microsoft WMI Scripting v11 Library

'添加 Command1

Private Sub Command1_Click()

Dim cpuSet As SWbemObjectSet

Dim cpu As SWbemObject

Set cpuSet = GetObject("winmgmts:{impersonationLevel=impersonate}")InstancesOf("Win32_Processor")

For Each cpu In cpuSet

MsgBox "CPU系列号:" & cpuProcessorId 取得CPU序列号

Next

End Sub

'---------------------------------------

如果没有那个Microsoft WMI Scripting v11 Library ,你也可以添加工程,添加两个command和一个text,然后代码:

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

Private Const VER_PLATFORM_WIN32_NT = 2

Private Const VER_PLATFORM_WIN32_WINDOWS = 1

Private Const VER_PLATFORM_WIN32s = 0

Private Sub Command1_Click()

Dim len5 As Long, aa As Long

Dim cmprName As String

Dim osver As OSVERSIONINFO

'取得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

Text1Text = mycpuProcessorId

Next

End Sub

Private Sub Command2_Click()

Unload Me

End Sub

Windows 9x下是通过读取注册表获取CPU占用,但是只能是整体,不可能细分到每个进程

Private Type LARGE_INTEGER

lowpart As Long

highpart As Long

End Type

Private Declare Function QueryPerformanceCounter Lib _

"kernel32" (lpPerformanceCount As LARGE_INTEGER) _

As Long

Private Declare Function QueryPerformanceFrequency _

Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long

Private Const REG_DWORD = 4 ' 32-bit number

Private Const HKEY_DYN_DATA = &H80000006

Private Declare Function RegQueryValueEx Lib _

"advapi32dll" Alias "RegQueryValueExA" (ByVal hKey _

As Long, ByVal lpValueName As String, ByVal _

lpReserved As Long, lpType As Long, lpData _

As Any, lpcbData As Long) As Long

Private Declare Function RegOpenKey Lib "advapi32dll" _

Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal _

lpSubKey As String, phkResult As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32dll" _

(ByVal hKey As Long) As Long

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Sub Form_DblClick()

'If the mouse is double clicked then end the

'program

End

End Sub

Private Sub Form_Load()

'Positions the form at the top right corner of

'the screen regardless of monitor size

Form1Top = 1

Form1Left = ScreenWidth - Form1Width

Call InitCPU

Call OnTop

End Sub

Private Sub SSPanel1_DblClick(Index As Integer)

Call Form_DblClick

End Sub

Private Sub Timer1_Timer()

Dim lData As Long, lType As Long, lSize As Long

Dim hKey As Long

Qry = RegOpenKey(HKEY_DYN_DATA, "PerfStats\StatData", hKey)

'If there's a problem accessing the registry

If Qry <> 0 Then

MsgBox "Can't Open Statistics Key"

End

End If

lType = REG_DWORD

lSize = 4

'Querying the registry for CPUUsage

Qry = RegQueryValueEx(hKey, "KERNEL\CPUUsage", _

0, lType, lData, lSize)

'clears the SSPanel boxes

Do Until SSPanel1(x)BackColor = &HC0C0C0

SSPanel1(x)BackColor = &HC0C0C0

x = x + 1

If x >= 10 Then Exit Do

Loop

'statbar is the variable that holds the CPU

'usage divided by 10

'(ex if 79% of the CPU is being used then

' statbar will hold the int(79) = 8)

statbar = Int(lData / 10)

If statbar >= 1 Then statbar = statbar - 1

'used to fill the SSPanel with the color green

'beginning with 0 and ending with the value of

'statbar

For fillall = 0 To statbar

SSPanel1(fillall)BackColor = &HFF&

Next fillall

If Int(lData / 10) = 0 Then SSPanel1(0)BackColor = &HC0C0C0

'Print lData

'Label2Caption = lData & "%"

Qry = RegCloseKey(hKey)

End Sub

Private Sub InitCPU()

Dim lData As Long, lType As Long, lSize As Long

Dim hKey As Long

Qry = RegOpenKey(HKEY_DYN_DATA, "PerfStats\StartStat", hKey)

If Qry <> 0 Then

MsgBox "Can't Open Statistics Key"

End

End If

lType = REG_DWORD

lSize = 4

Qry = RegQueryValueEx(hKey, "KERNEL\CPUUsage", 0, lType, lData, lSize)

Qry = RegCloseKey(hKey)

End Sub

Private Sub OnTop()

Const SWP_NOMOVE = &H2

Const SWP_NOSIZE = &H1

Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE

Const HWND_TOPMOST = -1

Const HWND_NOTOPMOST = -2

If SetWindowPos(Form1hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS) = True Then

success% = SetWindowPos(Form1hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)

End If

End Sub

Windows NT/2000 下

参考一下c的吧,没时间写成VB

主要是使用了Microsoft未公开的 NtQuerySystemInformation

需要ntdlldll

一个VB获取CPU占用率的代码:

>

VB可以使用Windows管理设备(WMI)进行读取计算机硬件信息。

Dim s, System, item

Dim i As Integer

Set System = GetObject("winmgmts:")InstancesOf("Win32_ComputerSystem")

For Each item In System

‘List1AddItem itemcputype

s = "Computer Info" & vbCrLf

s = s & "" & vbCrLf

s = s & "计算机名称: " & itemname & vbCrLf

s = s & "状态: " & itemStatus & vbCrLf

s = s & "类型: " & itemSystemType & vbCrLf

s = s & "生产厂家: " & itemManufacturer & vbCrLf

s = s & "型号: " & itemModel & vbCrLf

s = s & "内存: ~" & itemtotalPhysicalMemory \ 1024000 & "mb" & vbCrLf

s = s & "域: " & itemdomain & vbCrLf

‘s = s & "工作组" & itemWorkgroup & vbCrLf ‘获得工作组和域的选项不能同时用

s = s & "当前用户: " & itemusername & vbCrLf

s = s & "启动状态" & itemBootupState & vbCrLf

s = s & "该计算机属于" & itemPrimaryOwnerName & vbCrLf

s = s & "系统类型" & itemCreationClassName & vbCrLf

s = s & "计算机类类型" & itemDescription & vbCrLf

For i = 0 To 1 ‘这里假设安装了两个系统

s = s & Chr(5) & "启动选项" & i & " :" & itemSystemStartupOptions(i) _

& vbCrLf

Next i

Next

Text1Text = s

运行上面的代码后,在textBox中就可以显示计算机的信息。

在上面的代码中,程序通过GetObject("winmgmts:")获得WMI对象,然后获得下面的

Win32_ComputerSystem子项并通过访问Win32_ComputerSystem对象中的分项获得系统中的信息。

需要说明的是,并不是所有的系统都支持WMI,在有些系统中无法显示生产厂家等信息。

tmpstr=""

set fso=createObject("scriptingfilesystemObject")

Set dc = fsoDrives

for each d in dc

If disReady Then

    tmpstr=tmpstr & "磁盘" & dDriveLetter & ":" & vbcrlf &_

    "可用空间:" & (dAvailableSpace/1024/1024/1024) & " GB" & vbcrlf &_

    "空余空间:" & (dFreeSpace/1024/1024/1024) & " GB"  &vbcrlf &_

    "总空间大小:" & (dTotalSize/1024/1024/1024) & " GB"  &vbcrlf &_

    "文件系统:" & dFileSystem & vbcrlf &_

    "驱动器类型:" & dDriveType &vbcrlf &_

    "是否就绪:" & dIsReady &vbcrlf &_

    "路径:" & dPath &vbcrlf &_

    "根目录:" & dRootFolder &vbcrlf &_

    "序列号:" & dSerialNumber &vbcrlf &_

    "共享名:" & dShareName &vbcrlf &_

    "卷名:" & dVolumeName &vbcrlf &vbcrlf

 End If

Next

msgbox tmpstr

以上就是关于如何用VB读取计算机主板或CPU的机器码全部的内容,包括:如何用VB读取计算机主板或CPU的机器码、VB 查看进程CPU使用率、请教,如何用VB获取计算机硬件信息等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9335083.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存