QueryServiceStatusEx function

Retrieves the current status of the specified service based on the specified information level.

Syntax

BOOL QueryServiceStatusEx(
  SC_HANDLE      hService,
  SC_STATUS_TYPE InfoLevel,
  LPBYTE         lpBuffer,
  DWORD          cbBufSize,
  LPDWORD        pcbBytesNeeded
);

Parameters

hService

A handle to the service. This handle is returned by the CreateService or OpenService function, and it must have the SERVICE_QUERY_STATUS access right. For more information, see Service Security and Access Rights.

InfoLevel

The service attributes to be returned. Use SC_STATUS_PROCESS_INFO to retrieve the service status information. The lpBuffer parameter is a pointer to a SERVICE_STATUS_PROCESS structure.

Currently, no other information levels are defined.

lpBuffer

A pointer to the buffer that receives the status information. The format of this data depends on the value of the InfoLevel parameter.

The maximum size of this array is 8K bytes. To determine the required size, specify NULL for this parameter and 0 for the cbBufSize parameter. The function will fail and GetLastError will return ERROR_INSUFFICIENT_BUFFER. The pcbBytesNeeded parameter will receive the required size.

cbBufSize

The size of the buffer pointed to by the lpBuffer parameter, in bytes.

pcbBytesNeeded

A pointer to a variable that receives the number of bytes needed to store all status information, if the function fails with ERROR_INSUFFICIENT_BUFFER.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError. The following errors can be returned.

Return code Description
ERROR_INVALID_HANDLE
The handle is invalid.
ERROR_ACCESS_DENIED
The handle does not have the SERVICE_QUERY_STATUS access right.
ERROR_INSUFFICIENT_BUFFER
The buffer is too small for the SERVICE_STATUS_PROCESS structure. Nothing was written to the structure.
ERROR_INVALID_PARAMETER
The cbSize member of SERVICE_STATUS_PROCESS is not valid.
ERROR_INVALID_LEVEL
The InfoLevel parameter contains an unsupported value.
ERROR_SHUTDOWN_IN_PROGRESS
The system is shutting down; this function cannot be called.

Remarks

The QueryServiceStatusEx function returns the most recent service status information reported to the service control manager. If the service just changed its status, it may not have updated the service control manager yet.

The process identifier returned in the SERVICE_STATUS_PROCESS structure is valid provided that the state of the service is one of SERVICE_RUNNING, SERVICE_PAUSE_PENDING, SERVICE_PAUSED, or SERVICE_CONTINUE_PENDING. If the service is in a SERVICE_START_PENDING or SERVICE_STOP_PENDING state, however, the process identifier may not be valid, and if the service is in the SERVICE_STOPPED state, it is never valid.

Examples

For an example, see Starting a Service or Stopping a Service.

Requirements

   
Minimum supported client Windows XP [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Target Platform Windows
Header winsvc.h (include Windows.h)
Library Advapi32.lib
DLL Advapi32.dll

See Also

SERVICE_STATUS_PROCESS

Service Functions

Service Startup