Is there a way to stop an "NT Service" from a VB app?
Thanks
Printable View
Is there a way to stop an "NT Service" from a VB app?
Thanks
Sure! But not just any service. You can do this with the
WMI classes. Specifically, using the Win32_Service class
and it's StopService method.
Of course you need administrator rights to do this on NT/2000 - don't know about 9x.
Here's an ex. of how to enumerate the services.
Once you've got the Win32_Service Object for the service you want to stop just call the StopService method.Code:Public Sub listServices(Optional strServer As String = ".")
Dim objs As WbemScripting.SWbemObjectSet
Dim obj As WbemScripting.SWbemObject
Dim svcs As WbemScripting.SWbemServices
Dim swLocator As WbemScripting.SWbemLocator
Set swLocator = New WbemScripting.SWbemLocator
Set svcs = swLocator.ConnectServer(strServer)
Set objs = svcs.InstancesOf("Win32_Service")
Debug.Print Chr(10)
For Each obj In objs
Debug.Print "Display Name: " & obj.Properties_.Item("DisplayName")
Debug.Print "ProcessId: " & obj.Properties_.Item("ProcessId")
Debug.Print "StartMode: " & obj.Properties_.Item("StartMode")
Debug.Print "SystemName: " & obj.Properties_.Item("SystemName")
Debug.Print "StartName: " & obj.Properties_.Item("StartName")
Debug.Print "Can be Stopped: " & obj.Properties_.Item("AcceptStop")
Next obj
Set objs = Nothing
Set svcs = Nothing
Set swLocator = Nothing
End Sub
It's def is simply - Service.StopService() As Integer
good luck. ;)