使用WMI得到计算机的信息

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

使用WMI得到计算机信息

WMI是一项行业推荐规范,旨在为访问企业环境中的管理信息而开发一种标准化技术。该信息包括系统内存的状态、当前安装的客户应用程序清单,以及有关客户端状态的其他数据。WMI是可伸缩的系统管理结构,该规范采用一个统一、基于标准且可扩展的面向对象接口。它提供与系统管理员信息和基础WMI API交互的标准方法,主要由系统管理应用程序开发人员和系统管理员用来访问和操作系统管理信息WMI可用来生成组织和管理系统信息的工具,使系统管理人员能够更密切的监视系统活动。WMI提供了一套内置在Microsoft Windows操作系统中的丰富的系统管理服务,现在有大量的应用程序、服务和设备用其为信息技术操作和产品支持组织提供全方位的管理功能。基于WMI的管理系统的使用带来了更可靠的计算环境和更高的系统可靠性,从而节省了企业的开销。WMI提供的大量的规范为许多高端应用程序,例如Microsoft Exchange、Microsoft SQL Server和Microsoft Internet信息服务(IIS)等实现如下管理任务。1. 监视应用程序的运行情况2.  检测瓶颈或故障3.  管理和配置应用程序4.  查询应用程序数据(使用对象关系的遍历和查询)5.  执行无缝的本地或远程管理操作 下面我们通过一个例子来说明WMI的强大功能。以vb6为列:引用”Microsoft WMI Scripting V1.1 Library”代码如下:Option ExplicitDim WithEvents Sink As SWbemSinkDim j As Integer'功能:利用WMI组件得到计算机信息,每一个小功能分开写,便于大家查阅 Private Sub cmdDone_Click()Dim oWMINameSpace As SWbemServicesDim oLogicalDiskSet As SWbemObjectSetDim oLogicalDisk As SWbemObject Dim ObjSet As VariantDim sDrive As StringDim sValue As StringDim dblSize As DoubleDim Obj As Variant Dim lIndex As Long Set oWMINameSpace = GetObject("winmgmts:") '得到驱动器的信息On Error Resume NextSet 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) = sValueNext      CleanUp:Set oLogicalDisk = NothingSet oLogicalDiskSet = NothingSet oWMINameSpace = NothingExit Sub ErrorHandler:MsgBox "" & Err.Description GoTo CleanUp End Sub Private Sub Command1_Click()Unload MeEnd 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 SWbemServicesDim SystemSet As VariantDim System As VariantDim ObjSet As VariantDim 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.SerialNumberNext'cpuSet 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 SWbemServicesDim ObjSet As VariantDim Obj As VariantDim 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 = 0End 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 + 1End Sub 当执行后,出现如下所示: 是不是感到了WMI功能的强大。(完)注明:Windows2000/XP已经安装了WMI。Win98默认不支持,需要安装WMI For Win9x驱动。下载地址:http://www.microsoft.com/downloads/details.aspx?FamilyID=98a4c5ba-337b-4e92-8c18-a63847760ea5&DisplayLang=en <script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: vb
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭