Attribute VB_Name = "WMI在VB中的使用"
' WMI 的使用例程
' 需引用 MicroSoft WMI Scripting V1.2 Library
' 文件定位: C:/WINDOWS/system32/wbem/wbemdisp.TLB
Option Explicit
Private objSWbemLocator As SWbemLocator ' 创建 SWbemLocator 指针
Private objSWbemServices As SWbemServices ' 连接到 WMI 服务
Private objSWbemObjectSet As SWbemObjectSet ' 获得 WMI 托管资源(类)的实例集合
Private objSWbemObject As SWbemObject ' 枚举实例并显示属性
Private objSWbemProperty As SWbemProperty
Private objSWbemPropertySet As SWbemPropertySet
Private objSWbemMethod As SWbemMethod
Private objSWbemMethodSet As SWbemMethod
Private objSWbemQualifier As SWbemQualifier
Private strComputer As String, strNameSpace As String, strClass As String
' 启动 WMI ,建立服务对象的指针
Private Sub WMI_Start()
strComputer = "." ' 计算机名, “.”为本机
strNameSpace = "root/CIMV2" ' 命名空间
' 建立一个WBEM对象的引用指针
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
' 连接到指定计算机、命名空间的WMI,返回一个对 SWbemServices 对象的引用
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, strNameSpace)
End Sub
' 用 WMI 的 ExecQuery 查询方法获得实例
Public Sub WMI_ExecQuery()
strClass = "Win32_Process" ' 指定的类名
WMI_Start ' 启动 WMI,建立服务对象的指针
' 通过 WQL 的查询,返回指定类的指定实例
' 如:Set objSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM " & strClass & " WHERE Name = 'alg.exe'")
Set objSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM " & strClass & " WHERE Name = '" & 查找的内容 & "'")
' 枚举查询到的实例
For Each objSWbemObject In objSWbemObjectSet
With objSWbemObject
'
'
' 枚举过程
'
'
End With 'objSWbemObject
Next objSWbemObject
Set objSWbemObject = Nothing
Set objSWbemObjectSet = Nothing
End Sub
' 用 WMI 的 InstancesOf 方法获得所有实例
Public Sub WMI_InstancesOf()
strClass = "Win32_Process" ' 指定的类名
WMI_Start ' 启动 WMI,建立服务对象的指针
' 通过 InstancesOf 方法,返回指定类的所有实例
Set objSWbemObjectSet = objSWbemServices.InstancesOf(strClass)
' 枚举所有实例
For Each objSWbemObject In objSWbemObjectSet
With objSWbemObject
'
'
' 枚举过程
'
'
End With 'objSWbemObject
Next objSWbemObject
Set objSWbemObject = Nothing
Set objSWbemObjectSet = Nothing
End Sub
' 用 WMI Moniker 的 ExecQuery 查询方法获得实例
Public Sub WMI_Moniker_ExecQuery()
strComputer = "." ' 计算机名, “.”为本机
strNameSpace = "root/CIMV2" ' 命名空间
strClass = "Win32_Process" ' 指定的类名
' 建立指定计算机、命名空间的 WMI 的 SWbemServices 对象的引用
Set objSWbemServices = GetObject("winmgmts://" & strComputer & "/" & strNameSpace)
' 通过 WQL 的查询,返回指定类的指定实例
' 如:Set objSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM " & strClass & " WHERE Name = 'alg.exe'")
Set objSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM " & strClass & " WHERE Name = '" & 查找的内容 & "'")
' 枚举查询到的实例
For Each objSWbemObject In objSWbemObjectSet
With objSWbemObject
'
'
' 枚举过程
'
'
End With 'objSWbemObject
Next objSWbemObject
Set objSWbemObject = Nothing
Set objSWbemObjectSet = Nothing
End Sub
' 用 WMI Moniker 的 InstancesOf 方法获得所有实例
Public Sub WMI_Moniker_InstancesOf()
strComputer = "." ' 计算机名, “.”为本机
strNameSpace = "root/CIMV2" ' 命名空间
strClass = "Win32_Process" ' 指定的类名
' 建立指定计算机、命名空间的 WMI 的 SWbemServices 对象的引用
Set objSWbemServices = GetObject("winmgmts://" & strComputer & "/" & strNameSpace)
' 通过 InstancesOf 方法,返回指定类的所有实例
Set objSWbemObjectSet = objSWbemServices.InstancesOf(strClass)
' 枚举所有实例
For Each objSWbemObject In objSWbemObjectSet
With objSWbemObject
'
'
' 枚举过程
'
'
End With 'objSWbemObject
Next objSWbemObject
Set objSWbemObject = Nothing
Set objSWbemObjectSet = Nothing
End Sub
' 列出一个指定类的所有的属性名称,非实例内容
Public Sub WMI_List_Properties()
strComputer = "." ' 计算机名, “.”为本机
strNameSpace = "root/CIMV2" ' 命名空间
strClass = "Win32_Process" ' 指定的类名
' 建立指定计算机、命名空间、指定类的的引用
Set objSWbemObject = GetObject("winmgmts://" & strComputer & "/" & strNameSpace & ":" & strClass)
' 得到指定类的属性名集合
Set objSWbemPropertySet = objSWbemObject.Properties_
'枚举属性名
For Each objSWbemProperty In objSWbemPropertySet
'
'
'Debug.Print objSWbemProperty.Name
'
'
Next objSWbemProperty
Set objSWbemProperty = Nothing
Set objSWbemPropertySet = Nothing
Set objSWbemObject = Nothing
End Sub