Use WMI to obtain system services

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.