WMI is a scalable system management structure. This specification uses a unified, standard-based, and scalable object-oriented interface. It provides a standard way to interact with system administrators and basic WMI APIs. It is mainly used by system management application developers and system administrators to access and manage operating system information.
WMI can be used to generate tools for organizing and managing system information so that system administrators can monitor system activities more closely.
WMI provides a rich set of system management services built into the Microsoft Windows operating system, A large number of applications, services, and devices are now used to provide comprehensive management functions for information technology operations and product support organizations. The use of WMI-based management systems provides a more reliable computing environment and higher system reliability.
?????? Let's take a look at how to use the powerful features provided by WMI to browse, start, and close windows system services.
?
WINXP + VB6 + SP6
Create a VB project, add listview on the form, and add three button controls.
'ReferenceMicrosoft WMI scripting v1.2 Library
?
The Code is as follows:
Public locator as swbemlocator
Public services as swbemservices
'Dim owminamespace as swbemservices
?
'Reference Microsoft WMI scripting v1.2 Library
Private sub form_load ()
'Set owminamespace = GetObject ("winmgmts :")
??? Listview1.listitems. Clear
??? Listview1.columnheaders. add, "service name", 1000
??? Listview1.columnheaders. Add, "details", 4000
??? Listview1.columnheaders. add, "status", 1000
???
??? 'Instantiate
??? Set locator = new swbemlocator
??? 'Connect to the computer. "Yang" is the computer name.
??? Set services = locator. connectserver ("yang ")
End sub
?
Private sub commandementclick ()
Dim item as listitem
Set serviceset = GetObject ("winmgmts:"). instancesof ("win32_service ")
?
For each service in serviceset
??? Set item = listview1.listitems. Add (, Service. Name, Service. Name)
??? Item. subitems (1) = service. Description &""
??? Item. subitems (2) = service. State &""
Next
End sub
?
Private sub command2_click ()
??? Dim serviceobject as swbemobject
??? Dim servicename
???
??? On Error resume next
??? Servicename = listview1.selecteditem. Text
??? If err. Number = 0 then
??????? Set serviceobject = services. Get ("win32_service = '" & servicename &"'")
??????? Serviceobject. startservice
??? End if
End sub
?
Private sub command3_click ()
??? Dim serviceobject as swbemobject
??? Dim servicename
???
??? On Error resume next
??? Servicename = listview1.selecteditem. Text
??? If err. Number = 0 then
???
??????? Set serviceobject = services. Get ("win32_service = '" & servicename &"'")
??????? Serviceobject. stopservice
??? End if
End sub
?
After running, click the button to display the system service in the listview. You can select any item to start or close the operation.