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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Services 1

Status
Not open for further replies.

Ablecken

Programmer
Jun 5, 2001
130
0
0
US
I have been fooling around with services for a while and all I need now is to be able to check on the status of a service. I can restart, start, stop and pause services but for the life of me I cant figure out how to get the status. Any ideas? Am I missing something simple?
 
There may be an easier way but I use the QueryServiceStatus API i.e.

In a module:
Code:
Option Explicit

'API Constants
Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
' Service Control
Public Const SERVICE_CONTROL_STOP = &H1
Public Const SERVICE_CONTROL_PAUSE = &H2
' Service State - for CurrentState
Public Const SERVICE_STOPPED = &H1
Public Const SERVICE_START_PENDING = &H2
Public Const SERVICE_STOP_PENDING = &H3
Public Const SERVICE_RUNNING = &H4
Public Const SERVICE_CONTINUE_PENDING = &H5
Public Const SERVICE_PAUSE_PENDING = &H6
Public Const SERVICE_PAUSED = &H7
'Service Control Manager object specific access types
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SC_MANAGER_CONNECT = &H1
Public Const SC_MANAGER_CREATE_SERVICE = &H2
Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Public Const SC_MANAGER_LOCK = &H8
Public Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
Public Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
Public Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS Or SC_MANAGER_MODIFY_BOOT_CONFIG)
'Service object specific access types
Public Const SERVICE_QUERY_CONFIG = &H1
Public Const SERVICE_CHANGE_CONFIG = &H2
Public Const SERVICE_QUERY_STATUS = &H4
Public Const SERVICE_ENUMERATE_DEPENDENTS = &H8
Public Const SERVICE_START = &H10
Public Const SERVICE_STOP = &H20
Public Const SERVICE_PAUSE_CONTINUE = &H40
Public Const SERVICE_INTERROGATE = &H80
Public Const SERVICE_USER_DEFINED_CONTROL = &H100
Public Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS Or SERVICE_START Or SERVICE_STOP Or SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or SERVICE_USER_DEFINED_CONTROL)

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

Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long

Public Function ServiceStatus(ComputerName As String, ServiceName As String) As String
    Dim ServiceStat As SERVICE_STATUS
    Dim hSManager As Long
    Dim hService As Long
    Dim hServiceStatus As Long

    ServiceStatus = ""
    hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
    If hSManager <> 0 Then
        hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
        If hService <> 0 Then
            hServiceStatus = QueryServiceStatus(hService, ServiceStat)
            If hServiceStatus <> 0 Then
                Select Case ServiceStat.dwCurrentState
                Case SERVICE_STOPPED
                    ServiceStatus = &quot;Stopped&quot;
                Case SERVICE_START_PENDING
                    ServiceStatus = &quot;Start Pending&quot;
                Case SERVICE_STOP_PENDING
                    ServiceStatus = &quot;Stop Pending&quot;
                Case SERVICE_RUNNING
                    ServiceStatus = &quot;Running&quot;
                Case SERVICE_CONTINUE_PENDING
                    ServiceStatus = &quot;Coninue Pending&quot;
                Case SERVICE_PAUSE_PENDING
                    ServiceStatus = &quot;Pause Pending&quot;
                Case SERVICE_PAUSED
                    ServiceStatus = &quot;Paused&quot;
                Case Else
                    ServiceStatus = &quot;Unknown - ther service was probably not found&quot;
                End Select
            End If
            CloseServiceHandle hService
        End If
        CloseServiceHandle hSManager
    End If
End Function
On a form:
Code:
Private Sub Command1_Click()
    MsgBox ServiceStatus(&quot;&quot;, &quot;Alerter&quot;)
End Sub
All you have to do is pass the name of the service you wish to check.


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Try this:

Sub ListServices()
Dim MyService
Dim MyMachine

MyService = &quot;ServiceToCheck&quot;
MyMachine= &quot;MachineToCheck&quot;

For each Service in GetObject &_(&quot;winmgmts:impersonationLevel=impersonate}!//&quot; & MyMachine).InstancesOf (&quot;win32_service&quot;)

If Service.StartName = MyService Then
Echo MyService & &quot; is in the &quot; & Service.State & &quot; state!&quot;

End If
Next

End Sub



You can also get the following:
Service.Description
Service.PathName
Service.Status
Service.State
Service.StartMode
Service.StartName



Jamie Gillespie
j-gillespie@s-cheshire.ac.uk
 
But make sure that the target machines have WMI installed, else the above example will fail
 
thanx guys,
strongm, im sorry but i dont know what wmi is. could you explain?
 
Hi Everybody!
I've used the above code published by ca8msm in order to check status of several services on several machines.
I'm trying to get a status msg (string) by sending the following command

StatusReply = ServiceStatus(ComputerName, ServiceName)

If ServiceName is more then one word, for example: &quot;DHCP Server&quot; I get the following reply:
&quot;Unknown - ther service was probably not found&quot;
If the service name is one word for example: &quot;Alerter&quot;
Everything is just fine.

Does anyone know what should I do, or what am I doing wrong?

Thanks,
Gil Bar
gil@i-p-hi.com

 
OpenService API takes the service name as the second argument.

Are you sure that &quot;DHCP Server&quot; is the service name.

I ask, because it is easy to getconfused between the service name and the service display name

On my win 2000 server, the DHCP service's service display name is
DHCP Server and the service name is DHCPServer (note no space)

The fact that the code works frosay Alerter is that the the display name and service name are the same!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Hi mattKnight!
First let me say thank you for your fast reply!

Of course you are right and I have confused the Service name and the service display name...
Is there a place where I can find all of the service names correctly? (Removing the spaces isn't always working)
Thanks again.
Gil
gil@i-p-hi.com
 
Yep,

(win2k)

Open up the the administrative tools in Control panel, look for the services applet open that, select the service you want and look in its properties.

I am not sure about NT, but I guess it will be displayed in a similar fashion

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Do remember that all services' properties are stored also in registry, at :
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

from there you can determine a lot of usefull stuff like real name, real path, display name etc.

Also this is a rough way to "declare an application as service", or to start it on the SYSTEM user : just create a key for it, or modify an existing one to point to your file :eek:)
 
Don't even begin to think that simply making an entry in the Registry can make an application into a service...
 
strongm is right - there is tons more work involved in building a NT service than just a registry entry.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
i said and quote:
<<this is a rough way to "declare an application as service", or to start it on the SYSTEM user>>

note the terms "rough", the quote marks used, and the expression "Start it on the system user"

I did not ment to build a real NT service.
Just to run it quietly, on the system user, at every start of the system, unlogged, blah-blah ..... As if it would have been a service.
Most of the users are looking just for this, not for treating system events.
Ever been in need for a "discrete keylogger" ? Well, this is the way to do it.
 
>Well, this is the way to do it

It ain't the way I'd do it, even if I had need of a "discrete keylogger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top