大方脑袋 發表於 2019-4-29 17:02:33

VB中使用WMI获取系统硬件和软件有关信息

<p>WMI是英文Windows Management Instrumentation的简写,它的功能主要是:访问本地主机的一些信息和服务,可以管理远程计算机(当然你必须要拥有足够的权限),比如:重启,关机,关闭进程,创建进程等。</p>
<p>当然此文是适用于vbscript</p>
<p>微软官方的资料:</p>
<p>实例如下:</p>
<p>用WMI,先工程-引用 Microsoft WMI Scripting V1.1 Library</p>
<p>获取显卡/声卡/内存/操作系统的信息</p>
<p><strong>声卡信息</strong></p>
<div class="jb51code">
<pre class="brush:vb;">
Private Sub wmiSoundDeviceInfo()

Dim wmiObjSet As SWbemObjectSet
Dim obj As SWbemObject

Set wmiObjSet = GetObject(winmgmts:{impersonationLevel=impersonate}). _
InstancesOf(Win32_SoundDevice)
On Local Error Resume Next

For Each obj In wmiObjSet
MsgBox obj.ProductName
Next
End Sub</pre>
</div>
<p>显卡信息</p>
<div class="jb51code">
<pre class="brush:vb;">
Private Sub wmiVideoControllerInfo()

Dim wmiObjSet As SWbemObjectSet
Dim obj As SWbemObject

Set wmiObjSet = GetObject(winmgmts:{impersonationLevel=impersonate}). _
InstancesOf(Win32_VideoController)

On Local Error Resume Next

For Each obj In wmiObjSet
MsgBox obj.VideoProcessor
Next
End Sub</pre>
</div>
<p>内存信息</p>
<div class="jb51code">
<pre class="brush:vb;">
Private Sub wmiPhysicalMemoryInfo()

Dim wmiObjSet As SWbemObjectSet
Dim obj As SWbemObject

Set wmiObjSet = GetObject(winmgmts:{impersonationLevel=impersonate}). _
InstancesOf(Win32_PhysicalMemory)

On Local Error Resume Next

For Each objItem In wmiObjSet
Debug.Print BankLabel: & objItem.BankLabel
Debug.Print Capacity: & objItem.Capacity
Debug.Print Caption: & objItem.Caption
Debug.Print CreationClassName: & objItem.CreationClassName
Debug.Print DataWidth: & objItem.DataWidth
Debug.Print Description: & objItem.Description
Debug.Print DeviceLocator: & objItem.DeviceLocator
Debug.Print FormFactor: & objItem.FormFactor
Debug.Print HotSwappable: & objItem.HotSwappable
Debug.Print InstallDate: & objItem.InstallDate
Debug.Print InterleaveDataDepth: & objItem.InterleaveDataDepth
Debug.Print InterleavePosition: & objItem.InterleavePosition
Debug.Print Manufacturer: & objItem.Manufacturer
Debug.Print MemoryType: & objItem.MemoryType
Debug.Print Model: & objItem.Model
Debug.Print Name: & objItem.name
Debug.Print OtherIdentifyingInfo: & objItem.OtherIdentifyingInfo
Debug.Print PartNumber: & objItem.PartNumber
Debug.Print PositionInRow: & objItem.PositionInRow
Debug.Print PoweredOn: & objItem.PoweredOn
Debug.Print Removable: & objItem.Removable
Debug.Print Replaceable: & objItem.Replaceable
Debug.Print SerialNumber: & objItem.SerialNumber
Debug.Print SKU: & objItem.SKU
Debug.Print Speed: & objItem.Speed
Debug.Print Status: & objItem.Status
Debug.Print Tag: & objItem.Tag
Debug.Print TotalWidth: & objItem.TotalWidth
Debug.Print TypeDetail: & objItem.TypeDetail
Debug.Print Version: & objItem.Version
Next
End Sub
</pre>
</div>
<p>操作系统信息</p>
<div class="jb51code">
<pre class="brush:vb;">
Private Sub Command1_Click()
Dim wmiObjSet As SWbemObjectSet
Dim obj As SWbemObject
Dim msg As String
Dim dtb As String
Dim d As String
Dim t As String
Dim bias As Long
On Local Error Resume Next
Set wmiObjSet = GetObject(winmgmts:{impersonationLevel=impersonate}).InstancesOf(Win32_OperatingSystem)
For Each obj In wmiObjSet
MsgBox 你当前使用的系统是 & obj.Caption
Next
End Sub</pre>
</div>
<p>说明:</p>
<p>大家可能会发现一个规律,实际上WMI对信息的提取都是使用了WIN32_类库名这样的规律,下列表格就是微软的操作系统各种硬件类的描述</p>
<p>其它WMI管理的类的信息在</p>
<p>http://msdn.microsoft.com/library/default.asp&#63;url=/library/en-us/wmisdk/wmi/accessing_hardware_and_software_through_wmi.asp</p>
<p>可以找到,其中也还有部分示例代码</p>
<p><strong>简单Win_32类表</strong></p>
<p>Win32 Classes<br />
Microsoft&reg; Windows&reg; classes give you the means to manipulate a variety of objects. The following table identifies the categories of Windows classes.</p>
<p>Category Description <br />
Computer system hardware Classes that represent hardware related objects. <br />
Operating system Classes that represent operating system related objects. <br />
Installed applications Classes that represent software related objects. <br />
WMI service management Classes used to manage WMI. <br />
Performance counters Classes that represent formatted and raw performance data.</p>
<p><strong>硬件类</strong><br />
Computer System Hardware Classes<br />
he Cooling Devices subcategory groups classes that represent instrumentable fans, temperature probes, and refrigeration devices.</p>
<p>Class Description <br />
Win32_Fan Represents the properties of a fan device in the computer system. <br />
Win32_HeatPipe Represents the properties of a heat pipe cooling device. <br />
Win32_Refrigeration Represents the properties of a refrigeration device. <br />
Win32_TemperatureProbe Represents the properties of a temperature sensor (electronic thermometer).</p>
<p>Input Device Classes</p>
<p>The Input Devices subcategory groups classes that represent keyboards and pointing devices.</p>
<p>Class Description <br />
Win32_Keyboard Represents a keyboard installed on a Windows system. <br />
Win32_PointingDevice Represents an input device used to point to and select regions on the display of a Windows computer system.</p>
<p>Mass Storage Classes</p>
<p>Classes in the Mass Storage subcategory represent storage devices such as hard disk drives, CD-ROM drives, and tape drives.</p>
<p>Class Description <br />
Win32_AutochkSetting Represents the settings for the autocheck operation of a disk. <br />
Win32_CDROMDrive Represents a CD-ROM drive on a Windows computer system. <br />
Win32_DiskDrive Represents a physical disk drive as seen by a computer running the Windows operating system. <br />
Win32_FloppyDrive Manages the capabilities of a floppy disk drive. <br />
Win32_PhysicalMedia Represents any type of documentation or storage medium. <br />
Win32_TapeDrive Represents a tape drive on a Windows computer.</p>
<p>Motherboard, Controller, and Port Classes</p>
<p>The Motherboard, Controllers, and Ports subcategory groups classes that represent system devices. Examples include system memory, cache memory, and controllers.</p>
<p>Class Description <br />
Win32_1394Controller Represents the capabilities and management of a 1394 controller. <br />
Win32_1394ControllerDevice Relates the high-speed serial bus (IEEE 1394 Firewire) Controller and the CIM_LogicalDevice instance connected to it. <br />
Win32_AllocatedResource Relates a logical device to a system resource. <br />
Win32_AssociatedProcessorMemory Relates a processor and its cache memory. <br />
Win32_BaseBoard Represents a baseboard (also known as a motherboard or system board). <br />
Win32_BIOS Represents the attributes of the computer system&#39;s basic input/output services (BIOS) that are installed on the computer. <br />
Win32_Bus Represents a physical bus as seen by a Windows operating system. <br />
Win32_CacheMemory Represents cache memory (internal and external) on a computer system. <br />
Win32_ControllerHasHub Represents the hubs downstream from the universal serial bus (USB) controller. <br />
Win32_DeviceBus Relates a system bus and a logical device using the bus. <br />
Win32_DeviceMemoryAddress Represents a device memory address on a Windows system. <br />
Win32_DeviceSettings Relates a logical device and a setting that can be applied to it. <br />
Win32_DMAChannel Represents a direct memory access (DMA) channel on a Windows computer system. <br />
Win32_FloppyController Represents the capabilities and management capacity of a floppy disk drive controller. <br />
Win32_IDEController Represents the capabilities of an Integrated Drive Electronics (IDE) controller device. <br />
Win32_IDEControllerDevice Association class that relates an IDE controller and the logical device. <br />
Win32_InfraredDevice Represents the capabilities and management of an infrared device. <br />
Win32_IRQResource Represents an interrupt request line (IRQ) number on a Windows computer system. <br />
Win32_MemoryArray Represents the properties of the computer system memory array and mapped addresses. <br />
Win32_MemoryArrayLocation Relates a logical memory array and the physical memory array upon which it exists. <br />
Win32_MemoryDevice Represents the properties of a computer system&#39;s memory device along with it&#39;s associated mapped addresses. <br />
Win32_MemoryDeviceArray Relates a memory device and the memory array in which it resides. <br />
Win32_MemoryDeviceLocation Association class that relates a memory device and the physical memory on which it exists. <br />
Win32_MotherboardDevice Represents a device that contains the central components of the Windows computer system. <br />
Win32_OnBoardDevice Represents common adapter devices built into the motherboard (system board). <br />
Win32_ParallelPort Represents the properties of a parallel port on a Windows computer system. <br />
Win32_PCMCIAController Manages the capabilities of a Personal Computer Memory Card Interface Adapter (PCMCIA) controller device. <br />
Win32_PhysicalMemory Represents a physical memory device located on a computer as available to the operating system. <br />
Win32_PhysicalMemoryArray Represents details about the computer system&#39;s physical memory. <br />
Win32_PhysicalMemoryLocation Relates an array of physical memory and its physical memory. <br />
Win32_PNPAllocatedResource Represents an association between logical devices and system resources. <br />
Win32_PNPDevice Relates a device (known to Configuration Manager as a PNPEntity), and the function it performs. <br />
Win32_PNPEntity Represents the properties of a Plug and Play device. <br />
Win32_PortConnector Represents physical connection ports, such as DB-25 pin male, Centronics, and PS/2. <br />
Win32_PortResource Represents an I/O port on a Windows computer system. <br />
Win32_Processor Represents a device capable of interpreting a sequence of machine instructions on a Windows computer system. <br />
Win32_SCSIController Represents a small computer system interface (SCSI) controller on a Windows system. <br />
Win32_SCSIControllerDevice Relates a SCSI controller and the logical device (disk drive) connected to it. <br />
Win32_SerialPort Represents a serial port on a Windows system. <br />
Win32_SerialPortConfiguration Represents the settings for data transmission on a Windows serial port. <br />
Win32_SerialPortSetting Relates a serial port and its configuration settings. <br />
Win32_SMBIOSMemory Represents the capabilities and management of memory-related logical devices. <br />
Win32_SoundDevice Represents the properties of a sound device on a Windows computer system. <br />
Win32_SystemBIOS Relates a computer system (including data such as startup properties, time zones, boot configurations, or administrative passwords) and a system BIOS (services, languages, system management properties). <br />
Win32_SystemDriverPNPEntity Relates a Plug and Play device on the Windows computer system and the driver that supports the Plug and Play device. <br />
Win32_SystemEnclosure Represents the properties associated with a physical system enclosure. <br />
Win32_SystemMemoryResource Represents a system memory resource on a Windows system. <br />
Win32_SystemSlot Represents physical connection points including ports, motherboard slots and peripherals, and proprietary connections points. <br />
Win32_USBController Manages the capabilities of a universal serial bus (USB) controller. <br />
Win32_USBControllerDevice Relates a USB controller and the CIM_LogicalDevice instances connected to it. <br />
Win32_USBHub Represents the management characteristics of a USB hub.</p>
<p><br />
Networking Device Classes</p>
<p>The Networking Devices subcategory groups classes that represent the network interface controller, its configurations, and its settings.</p>
<p>Class Description <br />
Win32_NetworkAdapter Represents a network adapter on a Windows system. <br />
Win32_NetworkAdapterConfiguration Represents the attributes and behaviors of a network adapter. The class is not guaranteed to be supported after the ratification of the Distributed Management Task Force (DMTF) CIM network specification. <br />
Win32_NetworkAdapterSetting Relates a network adapter and its configuration settings.</p>
<p><br />
Power Classes</p>
<p>The Power subcategory groups classes that represent power supplies, batteries, and events related to these devices.</p>
<p>Class Description <br />
Win32_AssociatedBattery Relates a logical device and the battery it is using. <br />
Win32_Battery Represents a battery connected to the computer system. <br />
Win32_CurrentProbe Represents the properties of a current monitoring sensor (ammeter). <br />
Win32_PortableBattery Represents the properties of a portable battery, such as one used for a notebook computer. <br />
Win32_PowerManagementEvent Represents power management events resulting from power state changes. <br />
Win32_UninterruptiblePowerSupply Represents the capabilities and management capacity of an uninterruptible power supply (UPS). <br />
Win32_VoltageProbe Represents the properties of a voltage sensor (electronic voltmeter).</p>
<p><br />
Printing Classes</p>
<p>The Printing subcategory groups classes that represent printers, printer configurations, and print jobs.</p>
<p>Class Description <br />
Win32_DriverForDevice Relates a printer to a printer driver. <br />
Win32_Printer Represents a device connected to a Windows computer system that is capable of reproducing a visual image on a medium. <br />
Win32_PrinterConfiguration Defines the configuration for a printer device. <br />
Win32_PrinterController Relates a printer and the local device to which the printer is connected. <br />
Win32_PrinterDriver Represents the drivers for a Win32_Printer instance. <br />
Win32_PrinterDriverDll Relates a local printer and its driver file (not the driver itself). <br />
Win32_PrinterSetting Relates a printer and its configuration settings. <br />
Win32_PrintJob Represents a print job generated by a Windows application. <br />
Win32_TCPIPPrinterPort Represents a TCP/IP service access point.</p>
<p>Telephony Classes</p>
<p>The Telephony subcategory groups classes that represent plain old telephone modem devices and their associated serial connections.</p>
<p>Class Description <br />
Win32_POTSModem Represents the services and characteristics of a Plain Old Telephone Service (POTS) modem on a Windows system. <br />
Win32_POTSModemToSerialPort Relates a modem and the serial port the modem uses.</p>
<p><br />
Video and Monitor Classes</p>
<p>The Video and Monitors subcategory groups classes that represent monitors, video cards, and their associated settings.</p>
<p>Class Description <br />
Win32_DesktopMonitor Represents the type of monitor or display device attached to the computer system. <br />
Win32_DisplayConfiguration Represents configuration information for the display device on a Windows system. This class is obsolete. In place of this class, use the properties in the Win32_VideoController, Win32_DesktopMonitor, and CIM_VideoControllerResolution classes. <br />
Win32_DisplayControllerConfiguration Represents the video adapter configuration information of a Windows system. This class is obsolete. In place of this class, use the properties in the Win32_VideoController, Win32_DesktopMonitor, and CIM_VideoControllerResolution classes. <br />
Win32_VideoConfiguration This class has been eliminated from Windows XP and later; attempts to use it will generate a fatal error. In place of this class, use the properties contained in the Win32_VideoController, Win32_DesktopMonitor, and CIM_VideoControllerResolution classes. <br />
Win32_VideoController Represents the capabilities and management capacity of the video controller on a Windows computer system. <br />
Win32_VideoSettings Relates a video controller and video settings that can be applied to it.</p>
<p></p>
<p>而每一个类都有对应的数据结构如</p>
<p>显卡</p>
<p>class Win32_VideoController : CIM_PCVideoController<br />
{<br />
uint16 AcceleratorCapabilities[];<br />
string AdapterCompatibility;<br />
string AdapterDACType;<br />
uint32 AdapterRAM;<br />
uint16 Availability;<br />
string CapabilityDescriptions[];<br />
string Caption;<br />
uint32 ColorTableEntries;<br />
uint32 ConfigManagerErrorCode;<br />
boolean ConfigManagerUserConfig;<br />
string CreationClassName;<br />
uint32 CurrentBitsPerPixel;<br />
uint32 CurrentHorizontalResolution;<br />
uint64 CurrentNumberOfColors;<br />
uint32 CurrentNumberOfColumns;<br />
uint32 CurrentNumberOfRows;<br />
uint32 CurrentRefreshRate;<br />
uint16 CurrentScanMode;<br />
uint32 CurrentVerticalResolution;<br />
string Description;<br />
string DeviceID;<br />
uint32 DeviceSpecificPens;<br />
uint32 DitherType;<br />
datetime DriverDate;<br />
string DriverVersion;<br />
boolean ErrorCleared;<br />
string ErrorDescription;<br />
uint32 ICMIntent;<br />
uint32 ICMMethod;<br />
string InfFilename;<br />
string InfSection;<br />
datetime InstallDate;<br />
string InstalledDisplayDrivers;<br />
uint32 LastErrorCode;<br />
uint32 MaxMemorySupported;<br />
uint32 MaxNumberControlled;<br />
uint32 MaxRefreshRate;<br />
uint32 MinRefreshRate;<br />
boolean Monochrome;<br />
string Name;<br />
uint16 NumberOfColorPlanes;<br />
uint32 NumberOfVideoPages;<br />
string PNPDeviceID;<br />
uint16 PowerManagementCapabilities[];<br />
boolean PowerManagementSupported;<br />
uint16 ProtocolSupported;<br />
uint32 ReservedSystemPaletteEntries;<br />
uint32 SpecificationVersion;<br />
string Status;<br />
uint16 StatusInfo;<br />
string SystemCreationClassName;<br />
string SystemName;<br />
uint32 SystemPaletteEntries;<br />
datetime TimeOfLastReset;<br />
uint16 VideoArchitecture;<br />
uint16 VideoMemoryType;<br />
uint16 VideoMode;<br />
string VideoModeDescription;<br />
string VideoProcessor;<br />
};</p>
<p>声卡等</p>
<p>class Win32_SoundDevice : CIM_LogicalDevice<br />
{<br />
uint16 Availability;<br />
string Caption;<br />
uint32 ConfigManagerErrorCode;<br />
boolean ConfigManagerUserConfig;<br />
string CreationClassName;<br />
string Description;<br />
string DeviceID;<br />
uint16 DMABufferSize;<br />
boolean ErrorCleared;<br />
string ErrorDescription;<br />
datetime InstallDate;<br />
uint32 LastErrorCode;<br />
string Manufacturer;<br />
uint32 MPU401Address;<br />
string Name;<br />
string PNPDeviceID;<br />
uint16 PowerManagementCapabilities[];<br />
boolean PowerManagementSupported;<br />
string ProductName;<br />
string Status;<br />
uint16 StatusInfo;<br />
string SystemCreationClassName;<br />
string SystemName;<br />
};</p>
<p>class Win32_PrintJob : CIM_Job<br />
{<br />
string Caption;<br />
string DataType;<br />
string Description;<br />
string Document;<br />
string DriverName;<br />
datetime ElapsedTime;<br />
string HostPrintQueue;<br />
datetime InstallDate;<br />
uint32 JobId;<br />
string JobStatus;<br />
string Name;<br />
string Notify;<br />
string Owner;<br />
uint32 PagesPrinted;<br />
string Parameters;<br />
string PrintProcessor;<br />
uint32 Priority;<br />
uint32 Size;<br />
datetime StartTime;<br />
string Status;<br />
uint32 StatusMask;<br />
datetime TimeSubmitted;<br />
uint32 TotalPages;<br />
datetime UntilTime;<br />
};</p>
<p>如</p>
<p>Set wmiObjSet = GetObject(winmgmts:{impersonationLevel=impersonate}). _<br />
InstancesOf(Win32_PrintJob)</p>
<p>可以获得打印任务列表等信息</p>
<p>对于VB来讲获得系统硬件和软件信息大都使用API,现在使用WMI可以快速的实现我们的目标.如获取驱动器/显卡/共享信息等等都会变得方便和有效</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>VBS调用WMI遍历搜索硬盘文件并计数的方法</li><li>vbs通过WMI修改文件文件夹的NTFS权限</li><li>WMI StdRegProv 通过wmi操作注册表的vbscript实现代码 (本地或远程)</li><li>VBS通过WMI监视注册表变动的代码</li><li>VBS通过WMI获取CPU使用率的代码</li><li>将WMI中的DateTime类型转换成VBS时间的函数代码</li><li>VBS调用WMI实现搜索硬盘mp3文件</li><li>Rcmd.vbs 远程脚本</li><li>VBS调用WMI快速关闭IE的脚本</li><li>VBS脚本使用WMI操作注册表的代码</li><li>vbs wmi获取电脑硬件信息实例</li><li>初窥WMI_Vbs脚本编程简明教程补充读物</li><li>使用WMI得到计算机的信息</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: VB中使用WMI获取系统硬件和软件有关信息