Use WMI to get system service

zhaozj2021-02-16  192

WMI is a scalable system management structure that uses a unified, standard and scalable object-oriented interface. It provides standard methods with system administrator information and basic WMI API, mainly by system management application developers and system administrators to access and operating system management information.

WMI can be used to generate tools for organization and management system information, allowing system management personnel to monitor system activities.

WMI provides a built-in system management service built in the Microsoft Windows operating system, and now there is a large number of applications, services, and devices to provide a full range of management functions for information technology operations and product support organizations. The use of WMI-based management system has brought more reliable computing environments and higher system reliability.

?????? We look at how to achieve browsing, start and shut down Windows system services through the powerful features provided by WMI.

?

WinXP VB6 SP6

Create a new VB project, add ListView, 3 button controls on the Form.

'Quote Microsoft WMI Scripting V1.2 Library

?

code show as below:

Public Locator As Swbemlocator

Public Services As SwbemServices

'Dim Owminamespace As SwbemServices

?

'Quote Microsoft WMI Scripting V1.2 Library

Private sub flow_load ()

'Set owminamespace = getObject ("WinMgmts:")

??? listview1.listItems.clear

??? Listview1.columnheaders.add,, "service name", 1000

??? Listview1.columnheaders.add,, "Details", 4000

???????????????????????10

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? 'instantiation

??? set locator = new swbemlocator

??? 'connected to the computer, "yang" is the computer name

??? set services = locator.ConnectServer ("yang")

End Sub

?

Private submmand1_click ()

DIM ITEM AS LISTITEM

Set service = 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 fascist2_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 = '" ") ??????? serviceObject.StartService

??? End IF

End Sub

?

Private submmand3_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 = '" & Services)

??????? serviceObject.stopservice

??? End IF

End Sub

?

After running, click the button, and the system's service appears in ListView, then you can select any one, select Start or Close the operation.

转载请注明原文地址:https://www.9cbs.com/read-7935.html

New Post(0)