Make a Win32 API call to OpenSCManager to get a handle to the Service Control Manager. Declare the function like this:
Code:
Private Declare Function OpenSCManager Lib "advapi32" _
Alias "OpenSCManagerW" _
(ByVal lpMachineName As Long, _
ByVal lpDatabaseName As Long, _
ByVal dwDesiredAccess As Long) As Long
You are in good shape as long as the handle you get back is not zero. Next thing you do is get a handle to the service itself. Pass the handle of the Service Control Manager as the first argument to OpenService. You can delcare OpenService like this:
Code:
Private Declare Function OpenService Lib "advapi32" _
Alias "OpenServiceW" _
(ByVal hSCManager As Long, _
ByVal lpServiceName As Long, _
ByVal dwDesiredAccess As Long) As Long
Again you are doing good as long as you dont get a zero back... The final thing is to call ControlService and tell it to stop. You can declare ControlService like this:
Code:
Private Declare Function ControlService Lib "advapi32" _
(ByVal hService As Long, _
ByVal dwControl As Long, _
lpServiceStatus As SERVICE_STATUS) As Long
Oh and the final thing you need to know is that you can set the dwDesiredAccess level to 1 for the call to OpenSCManager and I think STOP access is 32 for the call to OpenService or you can just use 511 for all access (1+2+4+8+16+32+64+128+256)... Oh also pass 1 as the value of dwControl to stop it.
I guess you also need the type def for SERVICE_STATUS. You don't have to worry about the values if you are just stopping it. Just declare a variable of this type and pass it along to ControlService.
Code:
Type SERVICE_STATUS
dwServiceType As Long
dwCurrentState As Long
dwControlsAccepted As Long
dwWin32ExitCode As Long
dwServiceSpecificExitCode As Long
dwCheckPoint As Long
dwWaitHint As Long
End Type
Oh yeah, and one last final thing. You have to close the handles of both the service control manager and the service itself... Pass the handles for each to CloseServiceHandle, you can declare it like this:
Code:
Private Declare Function CloseServiceHandle Lib "advapi32" _
(ByVal hSCObject As Long) As Long
Hi,
Thanks for your post and apologies it has taken me so long to come back to you.
Above is very useful, thanks. Is it possible to check the service running on another machine, apart from the local machine?
Thanks again
Tim
Yes you can open the Service Control Manager on a remote machine if you just pass its name to the OpenSCManager function. You use the handle it returns the same as when calling the local machine.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.