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!

Service Status 1

Status
Not open for further replies.

Ablecken

Programmer
Jun 5, 2001
130
US
Hey all, I am using this to check the status of a service:

Code:
    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;Continue Pending&quot;
                    Case SERVICE_PAUSE_PENDING
                        ServiceStatus = &quot;Pause Pending&quot;
                    Case SERVICE_PAUSED
                        ServiceStatus = &quot;Paused&quot;
                    Case Else
                        ServiceStatus = &quot;Not found&quot;
                End Select
            End If
            CloseServiceHandle hService
        End If
        CloseServiceHandle hSManager
    End If

the problem i have is that the hService comes back with a zero even though i know that the service is there and running. Any ideas? Any way to make it better?
 
It may be a permissions issue.

>>Before granting the requested access, the system checks the access token of the calling process against the discretionary access-control list of the security descriptor associated with the service object.<<

Try opening the service with GENERIC_READ instead of SERVICE_ALL_ACCESS. GENERIC_READ is a combination of four flags:

(STANDARD_RIGHTS_READ Or SERVICE_QUERY_CONFIG Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS)

You could also call GetLastError after OpenService fails to confirm ERROR_ACCESS_DENIED

There's also a code example on my MSDN CD which I haven't compared with yours, I'll leave that to you!
Paul Bent
Northwind IT Systems
 
Oops, spoke too soon. the getlasterror, is that an api?
 
That's right

Private Declare Function GetLastError Lib &quot;kernel32&quot; () As Long



peterguhl@yahoo.de
 
There are two functions, GetLastError returns the number and FormatMessage returns the error message:
Code:
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000

Public Declare Function GetLastError Lib &quot;kernel32.dll&quot; () As Long

Public Declare Function FormatMessage Lib &quot;kernel32&quot; _
Alias &quot;FormatMessageA&quot; (Byval dwFlags As Long, _
lpSource As Any, Byval dwMessageId As Long, _
Byval dwLanguageId As Long, Byval lpBuffer As String, _
Byval nSize As Long, Arguments As Long) As Long
'____________________________________
Public Function fGetLastErrorDetails(strErrMsg As String) As Long

 '--- Returns the error code raised by a failed API call,
 '    else 0 if no error occurred
 '--- Places the error msg in the passed variable strErrMsg

 Dim plngRtn As Long   'Return value
 Dim plngBuffLen As Long  'Length of string buffer
 Dim pstrRtn As String  'String buffer to return the error msg
 Dim plngErrCode As Long 'Error code returned

 'Get the last error code from Windows
 plngRtn = GetLastError()
 If Not plngRtn = 0 Then
  plngErrCode = plngRtn
  'Get the error text from Windows
  pstrRtn = Space$(256)
  plngBuffLen = 256
  plngRtn = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, Clng(0), _
  plngErrCode, Clng(0), pstrRtn, plngBuffLen, Clng(0))
  If Not plngRtn = 0 Then
   strErrMsg = Left$(pstrRtn, plngRtn)
  Else
   strErrMsg = &quot;No error message found.&quot;
  End If
  fGetLastErrorDetails = plngErrCode
 Else
  'No error occurred
  strErrMsg = &quot;&quot;
  fGetLastErrorDetails = 0
 End If

End Function

Paul Bent
Northwind IT Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top