在VB中要使用Scripting API for WMI,必须引用 Microsoft WMI Scripting V1.1 Library
下面介绍Scripting API For WMI的几个对象
SWbemLocator——用于取得SWbemServices对象,他代表了本地或远程计算机上名字空间的一个连接。
SWbemService——代表名字空间的一个连接,可用于处理它的部件
SWbemObject——代表一个单独的类定义或一个对象实例
SWbemOjbectSet——包括SWbemObject的集合
下面是DNS WMI Provider的几个对象
MicrosoftDNS_Zone——用于管理DNS服务器上的区域的类
MicrosoftDNS_AType,MicrosoftDNS_CNAMEType,MicrosoftDNS_MXType等等——管理DNS Server上的各种资源记录
详细的参考请见MSDN,我用的是VS.NET2003带的MSDN
Scripting API for WMI的路径是 MSDN Library--设置和系统管理--Windows Management Instrumentation(WMI)--SDK文档--WMI Reference--Scripting API For WMI
DNS WMI Provider的路径是 MSDN Library--网络和目录服务--域名系统(DNS)--SDK文档--DNS WMI Provider--DNS WMI Provider Reference--DNS WMI Classes
下面是代码实现
需要引用Microsoft Scripting Runtime和Microsoft WMI Scripting V1.1 Library,只是示例了A、MX、和CName记录的操作,还可以扩展其他资源记录的操作,也可以加上区域的操作,参考MSDN就可以了
Class DNSController
Private objService As Object
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
osName As String
End Type
Private Function GetWindowsVersion() As OSVERSIONINFO
Dim ver As OSVERSIONINFO
ver.dwOSVersionInfoSize = 148
GetVersionEx ver
With ver
Select Case .dwPlatformId
Case 1
Select Case .dwMinorVersion
Case 0
.osName = "Windows 95"
Case 10
.osName = "Windows 98"
Case 90
.osName = "Windows Mellinnium"
End Select
Case 2
Select Case .dwMajorVersion
Case 3
.osName = "Windows NT 3.51"
Case 4
.osName = "Windows NT 4.0"
Case 5
If .dwMinorVersion = 0 Then
.osName = "Windows 2000"
ElseIf .dwMinorVersion = 1 Then
.osName = "Windows XP"
Else
.osName = "Windows 2003"
End If
End Select
Case Else
.osName = "Failed"
End Select
End With
GetWindowsVersion = ver
End Function
'判断操作系统,由于WMI在2003和2000上的实现略有差异,所以需要判断操作系统
Private Function IsWin2k3() As Boolean
Dim v As OSVERSIONINFO
v = GetWindowsVersion()
If v.osName = "Windows 2003" Then
IsWin2k3 = True
Else
IsWin2k3 = False
End If
End Function