Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if NT service is running?

Status
Not open for further replies.

TimGoff

Technical User
Jul 16, 2002
166
GB
Hi,

Could anybody suggest a cheeky bit of code that would check if an NT service is running on a specific machine please?

Thanks
Tim
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top