WMI (Windows Management Instrumentation) technology is a system management tool under Windows provided by Microsoft. Through this tool, almost all information in the client system can be managed locally or on the client system. Many professional network management tools are developed based on WMI. This tool is a standard tool under Win2000 and WinNT, and an extended installation option under Win9X. This article will introduce how to access WMI object programming through VB programming.
First, let’s look at a simple example of obtaining system information through WMI. This example obtains the processes running in the system through WMI objects:
FunctionEnum1()AsString
DimWMI
SetWMI=GetObject(WinMgmts:)
Setobjs=WMI.InstancesOf(Win32_PRocess)
ForEachobjInobjs
Enum1=Enum1 obj.Description Chr(13) Chr(10)
Next
EndFunction
In the above code, the WMI object is first obtained through GetObject(WinMgmts:). There are many sub-items under the WMI object. Here we obtain all the process list sub-items in the system through WMI.InstancesOf(Win32_Process).
Let's look at a complete example of accessing WMI objects. This example obtains computer information.
Create a new project, add a TextBox control and a CommandButton control to Form1, and write the following code in the Click event of the CommandButton:
PrivateSubCommand1_Click()
Dims,System,item
DimiAsInteger
SetSystem=GetObject(winmgmts:).InstancesOf(Win32_ComputerSystem)
ForEachitemInSystem
'List1.AddItemitem.cputype
s=ComputerInfo&vbCrLf
s=s&****************************&vbCrLf
s=s&computer name:&item.name&vbCrLf
s=s&Status:&item.Status&vbCrLf
s=s&type:&item.SystemType&vbCrLf
s=s&Manufacturer:&item.Manufacturer&vbCrLf
s=s&model:&item.Model&vbCrLf
s=s&memory:~&item.totalPhysicalMemory/1024000&mb&vbCrLf
s=s&domain:&item.domain&vbCrLf
's=s&Workgroup&item.Workgroup&vbCrLf' option to obtain workgroup and domain cannot be used at the same time
s=s¤t user:&item.username&vbCrLf
s=s&boot state&item.BootupState&vbCrLf
s=s&This computer belongs to&item.PrimaryOwnerName&vbCrLf
s=s&system type&item.CreationClassName&vbCrLf
s=s&Computer type&item.Description&vbCrLf
Fori=0To1'Here it is assumed that two systems are installed
s=s&Chr(5)&Startup Options&i&:&item.SystemStartupOptions(i)_
&vbCrLf
Nexti
Next
Text1.Text=s
EndSub
Run the program, click Command1, and the computer information will be displayed in the textBox.
In the above code, the program obtains the WMI object through GetObject(winmgmts:), then obtains the following Win32_ComputerSystem subkey and obtains the information in the system by accessing the sub-items in the Win32_ComputerSystem object.
It should be noted that not all systems support WMI, and in some systems information such as manufacturer cannot be displayed.
Today's computers and networks are very complex. For example, in terms of system hardware, there are motherboards, hard drives, network cards...
In terms of software, there are operating systems, software installed in the system, running processes, etc. Network aspects include domains, workgroups, etc. You can use WMI to access all the above information, but it will be very troublesome to access it using the same items as above. To this end, WMI provides a query statement similar to an SQL statement, through which the sub-items under the WMI object can be obtained.
Here is a code that loops through the network cards installed in the system and returns the MAC address of the network card:
PrivateFunctionMACAddress()AsString
Setobjs=GetObject(winmgmts:).ExecQuery(_
SELECTMACAddress&_
FROMWin32_NetworkAdapter&_
WHERE&_
((MACAddressIsNotNULL)&_
AND(Manufacturer<>&_
'Microsoft')))
ForEachobjInobjs
MACAddress=obj.MACAddress
ExitFor
Nextobj
EndFunction
The above code obtains the WMI object, and then runs ExecQuery to execute a WMI query statement to obtain the installed network card and return the MAC address of the network card.
WMI also supports event processing, allowing programs to handle system events, such as program running and closing, insertion and removal of removable drives, etc. Below is a program that can monitor the programs running on the system.
First create a new project, then click the project references item in the menu, select MicrosoftWMIScriptingLibrary in the references list, and add the WMI object library to the project. Then add a ListBox control to Form1, and then add the following code to Form1:
OptionExplicit
DimLocatorAsSWbemLocator
DimServicesAsSWbemServices
DimWithEventsStatusSinkAsSWbemSink
PrivateSubKillEvents()
StatusSink.Cancel
SetStatusSink=Nothing
EndSub
PrivateSubForm_Load()
DimQueryAsString
SetStatusSink=NewSWbemSink
SetLocator=CreateObject(WbemScripting.SWbemLocator)
SetServices=Locator.ConnectServer()
Query=SELECT*FROM__InstanceCreationEvent
Query=Query WITHIN1
Query=Query WHERETargetInstanceISA'Win32_Process'
Services.ExecNotificationQueryAsyncStatusSink,Query
EndSub
PrivateSubStatusSink_OnObjectReady(ByValStatusEventAsSWbemObject,_
ByValEventContextAsSWbemNamedValueSet)
Dimarr
DimstrQueAsString
DimiAsInteger
List1.Clear
arr=Split(StatusEvent.GetObjectText_,Chr(10))
Fori=LBound(arr)ToUBound(arr)
List1.AddItemarr(i)
Nexti
EndSub
PrivateSubStatusSink_OnCompleted(ByValHResultAsWbemErrorEnum,_
ByValErrorObjectAsSWbemObject,_
ByValEventContextAsSWbemNamedValueSet)
IfHResult<>wbemErrCallCancelledThen
'Error handling
EndIf
EndSub
In the above program, a SWbemSink object StatusSink is defined, then a SWbemServices object Server is created, and StatusSink is connected to the Server object. In this way, the running of the program can be monitored through StatusSink.
Run the program, and then run any program, and the information of the running program can be listed in the ListBox of Form1.
The most powerful aspect of WMI application is that it can realize remote management through WEB page. Next, let's create an HTML page, which can dynamically monitor the programs running in the system like the VB program above. The HTML code for program running in the monitoring system is as follows:
<html>
<head>
<objectID=mysinkCLASSID=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></object>
</head>
<SCRIPT>
functionwindow.onload()
{
varlocator=newActiveXObject(WbemScripting.SWbemLocator);
varservice=locator.ConnectServer();
szQuery=SELECT*FROM__InstanceCreationEvent;
szQuery =WITHIN1;
szQuery =WHERETargetInstanceISA'Win32_Process';
service.ExecNotificationQueryAsync(mysink,szQuery);
}
</SCRIPT>
<scriptFOR=mysinkEVENT=OnObjectReady(obj,objAsyncContext)>
document.all.info.innerHTML =obj.TargetInstance.Name <br>;
</script>
<body>
<spanID=info></span>
</body>
</html>
Save the page file with code as Htm suffix. Double-click to open the web page, and then run a program. The file names of the running programs can be listed on the web page.
The above briefly introduces the application of WMI. In fact, the operation of WMI objects is very complex and the functions are also very powerful. For example, you can use WMI to monitor the computers on the entire LAN on the server and install software in batches to the computers on the LAN ( such as anti-virus software). Access the server remotely through the page, control the server to run programs, add users, etc. For more WMI applications, readers can visit the WMI development homepage on MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmistart_5kth.asp
Get more information.
->