Results 1 to 7 of 7

Thread: Status of a Win2000 Service

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 1999
    Location
    Ohio
    Posts
    59

    Status of a Win2000 Service

    Does anyone now how I can use the API to check to see the status of a service?

    I need to write a program that checks the service every so often and if the status is paused or stopped I need to send an alert.

    Thanks.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
    2.  
    3. ' Service Control Manager object specific access types
    4. Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
    5. Public Const SC_MANAGER_CONNECT = &H1
    6. Public Const SC_MANAGER_CREATE_SERVICE = &H2
    7. Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
    8. Public Const SC_MANAGER_LOCK = &H8
    9. Public Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
    10. Public Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
    11.  
    12. Public Const SC_MANAGER_ALL_ACCESS =  _
    13.         (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT _
    14.         Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE _
    15.         Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS _
    16.         Or SC_MANAGER_MODIFY_BOOT_CONFIG)
    17.  
    18. 'Service object specific access types
    19. Public Const SERVICE_QUERY_CONFIG = &H1
    20. Public Const SERVICE_CHANGE_CONFIG = &H2
    21. Public Const SERVICE_QUERY_STATUS = &H4
    22. Public Const SERVICE_ENUMERATE_DEPENDENTS = &H8
    23. Public Const SERVICE_START = &H10
    24. Public Const SERVICE_STOP = &H20
    25. Public Const SERVICE_PAUSE_CONTINUE = &H40
    26. Public Const SERVICE_INTERROGATE = &H80
    27. Public Const SERVICE_USER_DEFINED_CONTROL = &H100
    28.  
    29. Public Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
    30.         SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG _
    31.         Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS _
    32.         Or SERVICE_START Or SERVICE_STOP Or _
    33.         SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or _
    34.         SERVICE_USER_DEFINED_CONTROL)
    35.  
    36. ' Service State -- for CurrentState
    37. Public Const SERVICE_STOPPED = &H1
    38. Public Const SERVICE_START_PENDING = &H2
    39. Public Const SERVICE_STOP_PENDING = &H3
    40. Public Const SERVICE_RUNNING = &H4
    41. Public Const SERVICE_CONTINUE_PENDING = &H5
    42. Public Const SERVICE_PAUSE_PENDING = &H6
    43. Public Const SERVICE_PAUSED = &H7
    44.  
    45.  
    46. Public Type SERVICE_STATUS
    47.     dwServiceType As Long
    48.     dwCurrentState As Long
    49.     dwControlsAccepted As Long
    50.     dwWin32ExitCode As Long
    51.     dwServiceSpecificExitCode As Long
    52.     dwCheckPoint As Long
    53.     dwWaitHint As Long
    54. End Type
    55.  
    56. Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" _
    57.         (ByVal lpMachineName As String, ByVal lpDatabaseName As String, _
    58.         ByVal dwDesiredAccess As Long) As Long
    59.  
    60. Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" _
    61.         (ByVal hSCManager As Long, ByVal lpServiceName As String, _
    62.         ByVal dwDesiredAccess As Long) As Long
    63.  
    64. Declare Function QueryServiceStatus Lib "advapi32.dll" _
    65.         (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
    66.  
    67. ' Here's where we test if the service is running
    68.  
    69. Dim lhSCManager As Long
    70. Dim lhService As Long
    71. Dim lhServiceStatus As Long
    72.  
    73. lhSCManager = OpenSCManager("Computer Name", SERVICES_ACTIVE_DATABASE,
    74. SC_MANAGER_ALL_ACCESS)
    75. If lhSCManager <> 0 Then
    76.     lhService = OpenService(lhSCManager, "Service Name",
    77.     SERVICE_ALL_ACCESS)
    78.     If lhService <> 0 Then
    79.         lhServiceStatus = QueryServiceStatus(lhService, ServiceStatus)
    80.         If lhServiceStatus <> 0 Then
    81.             Select Case ServiceStatus.dwCurrentState
    82.                 Case SERVICE_STOPPED
    83.                         'do something
    84.                 Case SERVICE_START_PENDING
    85.                         'do something
    86.                 Case SERVICE_STOP_PENDING
    87.                         'do something                
    88.                 Case SERVICE_RUNNING
    89.                         'do something                
    90.                 Case SERVICE_CONTINUE_PENDING
    91.                         'do something                
    92.                 Case SERVICE_PAUSE_PENDING
    93.                         'do something                
    94.                 Case SERVICE_PAUSED
    95.                         'do something                
    96.             End Select
    97.             CloseServiceHandle lhServiceStatus
    98.         End If
    99.         CloseServiceHandle lhService
    100.     End If
    101.     CloseServiceHandle lhSCManager
    102. End If

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 1999
    Location
    Ohio
    Posts
    59
    Hack,

    Thanks for the code. However, I'm getting a compile error: ByRef argument type mismatch.

    It highlights "ServiceStatus" in the following line of code.
    Code:
    lhServiceStatus = QueryServiceStatus(lhService, ServiceStatus)
    Do you know what's causing this?

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Hmmm...I've used this routine before with no problems. I assume you had to tailor it to fit your indidual needs. Post exactly what you are using, and how you are using it, and I'll take a look.

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 1999
    Location
    Ohio
    Posts
    59
    At first I had two modules, one with the declarations and it called the function in the second module. I put all the code in the main module so when it executed the first thing it does is check the status.

    Did I miss a setting I was suppose to change with my own data?

    Here's the code
    Code:
    Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
    
    ' 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)
    
    ' 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
    
    
    Public 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 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
    
    ' Check the state of the service
    '  CheckServiceState
    
    ' Here's where we test if the service is running
    Sub main()
    
    Dim lhSCManager As Long
    Dim lhService As Long
    Dim lhServiceStatus As Long
    
    lhSCManager = OpenSCManager("JW2K", SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
    If lhSCManager <> 0 Then
        lhService = OpenService(lhSCManager, "ULockService", SERVICE_ALL_ACCESS)
        If lhService <> 0 Then
            lhServiceStatus = QueryServiceStatus(lhService, ServiceStatus)
            If lhServiceStatus <> 0 Then
                Select Case ServiceStatus.dwCurrentState
                    Case SERVICE_STOPPED
                            'do something
                            MsgBox "The lock service is stopped!", vbCritical
                    Case SERVICE_START_PENDING
                            'do something
                    Case SERVICE_STOP_PENDING
                            'do something
                    Case SERVICE_RUNNING
                            'do something
                    Case SERVICE_CONTINUE_PENDING
                            'do something
                    Case SERVICE_PAUSE_PENDING
                            'do something
                    Case SERVICE_PAUSED
                            'do something
                            MsgBox "The lock service is paused!", vbCritical
                End Select
                CloseServiceHandle lhServiceStatus
            End If
            CloseServiceHandle lhService
        End If
        CloseServiceHandle lhSCManager
    End If
    
    End Sub

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Take these out of Sub Main
    Dim lhSCManager As Long
    Dim lhService As Long
    Dim lhServiceStatus As Long

    and make them Public variables instead of procedure level variables.

    How are you calling Sub Main?

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 1999
    Location
    Ohio
    Posts
    59
    Sub Main is my start up procedure.

    I made the variables public and received the same error.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width