用WMI得到计算机的信息的案例分享

2022-11-06 19:08:05 浏览数 (1)

下面我们通过一个例子来说明WMI的强大功能。以vb6为列:

引用”Microsoft WMI Scripting V1.1 Library”

代码如下:

Option Explicit

Dim WithEvents Sink As SWbemSink

Dim j As Integer

'功能:利用wmi组件得到计算机的信息,每一个小功能分开写,便于大家查阅

Private Sub cmdDone_Click()

Dim oWMINameSpace As SWbemServices

Dim oLogicalDiskSet As SWbemObjectSet

Dim oLogicalDisk As SWbemObject

Dim ObjSet As Variant

Dim sDrive As String

Dim sValue As String

Dim dblSize As Double

Dim Obj As Variant

Dim lIndex As Long

Set oWMINameSpace = GetObject("winmgmts:")

'得到驱动器的信息

On Error Resume Next

Set ObjSet = oWMINameSpace.InstancesOf("Win32_DiskDrive")

For Each Obj In ObjSet

List5.AddItem Obj.Caption & " - " & BytesToMegabytes(Obj.Size) & " GB"

Next

'得到每一个驱动器的详细信息

On Error GoTo ErrorHandler

'Set oWMINameSpace = GetObject("winmgmts:")

Set oLogicalDiskSet = oWMINameSpace.InstancesOf("Win32_LogicalDisk")

For Each oLogicalDisk In oLogicalDiskSet

On Error Resume Next

sDrive = oLogicalDisk.deviceid

ListView1.ListItems.Add , , sDrive

lIndex = ListView1.ListItems.Count

sValue = oLogicalDisk.Description & ""

ListView1.ListItems(lIndex).SubItems(1) = sValue

sValue = oLogicalDisk.FileSystem & ""

ListView1.ListItems(lIndex).SubItems(2) = sValue

sValue = oLogicalDisk.VolumeName & ""

ListView1.ListItems(lIndex).SubItems(3) = sValue

sValue = oLogicalDisk.VolumeSerialNumber & ""

ListView1.ListItems(lIndex).SubItems(4) = sValue

sValue = oLogicalDisk.Size & ""

If IsNumeric(sValue) Then

dblSize = BytesToMegabytes(CDbl(sValue))

sValue = CStr(dblSize) & " MB"

End If

ListView1.ListItems(lIndex).SubItems(5) = sValue

Next

CleanUp:

Set oLogicalDisk = Nothing

Set oLogicalDiskSet = Nothing

Set oWMINameSpace = Nothing

Exit Sub

ErrorHandler:

MsgBox "" & Err.Description

GoTo CleanUp

End Sub

Private Sub Command1_Click()

Unload Me

End Sub

Private Function BytesToMegabytes(Bytes As Double) As Double

Dim dblAns As Double

dblAns = (Bytes / 1024) / 1024

BytesToMegabytes = Format(dblAns, "###,###,##0.00")

End Function

Private Sub Command2_Click()

Dim oWMINameSpace As SWbemServices

Dim SystemSet As Variant

Dim System As Variant

Dim ObjSet As Variant

Dim Obj As Variant

Set oWMINameSpace = GetObject("winmgmts:")

'操作系统

Set SystemSet = oWMINameSpace.InstancesOf("Win32_OperatingSystem")

For Each System In SystemSet

List1.AddItem System.Caption

List1.AddItem System.Manufacturer

List1.AddItem System.BuildType & “” ‘Win9x下好像取不出来

List1.AddItem System.Version

List1.AddItem System.SerialNumber

Next

'cpu

Set ObjSet = oWMINameSpace.InstancesOf("Win32_Processor")

For Each Obj In ObjSet

List2.AddItem Obj.Caption

List2.AddItem Obj.currentclockspeed & " Mhz"

Next

End Sub

Private Sub Command3_Click()

Dim oWMINameSpace As SWbemServices

Dim ObjSet As Variant

Dim Obj As Variant

Dim Adapter As Variant

'内存

Set oWMINameSpace = GetObject("winmgmts:")

Set ObjSet = oWMINameSpace.InstancesOf("Win32_PhysicalMemory")

Dim i As String

For Each Obj In ObjSet

List3.AddItem BytesToMegabytes(Obj.capacity) & " MB" & " Chip"

Next

'网卡

Set Sink = New SWbemSink

Set Adapter = GetObject("winmgmts:")

Adapter.InstancesOfAsync Sink, "Win32_NetworkAdapter"

End Sub

Private Sub Form_Load()

j = 0

End Sub

Private Sub Sink_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)

Dim Adapter As Variant

‘得到所有的适配器信息

Set Adapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & j & "")

List4.AddItem Adapter.Description

If IsNull(Adapter.MACAddress) Then

List4.AddItem "No MAC Address"

List4.AddItem ""

Else

List4.AddItem "Mac: " & Adapter.MACAddress

List4.AddItem ""

End If

j = j 1

End Sub

0 人点赞