0 out of 5 rated this helpful Rate this topic

RegisterServiceCtrlHandlerEx function

Registers a function to handle extended service control requests.

Syntax

SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerEx(
  __in      LPCTSTR lpServiceName,
  __in      LPHANDLER_FUNCTION_EX lpHandlerProc,
  __in_opt  LPVOID lpContext
);

Parameters

lpServiceName [in]

The name of the service run by the calling thread. This is the service name that the service control program specified in the CreateService function when creating the service.

lpHandlerProc [in]

A pointer to the handler function to be registered. For more information, see HandlerEx.

lpContext [in, optional]

Any user-defined data. This parameter, which is passed to the handler function, can help identify the service when multiple services share a process.

Return value

If the function succeeds, the return value is a service status handle.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

The following error codes can be set by the service control manager.

Return codeDescription
ERROR_NOT_ENOUGH_MEMORY

Not enough memory is available to convert an ANSI string parameter to Unicode. This error does not occur for Unicode string parameters.

ERROR_SERVICE_NOT_IN_EXE

The service entry was specified incorrectly when the process called the StartServiceCtrlDispatcher function.

 

Remarks

The ServiceMain function of a new service should immediately call the RegisterServiceCtrlHandlerEx function to register a control handler function with the control dispatcher. This enables the control dispatcher to invoke the specified function when it receives control requests for this service. For a list of possible control codes, see HandlerEx. The threads of the calling process can use the service status handle returned by this function to identify the service in subsequent calls to the SetServiceStatus function.

The RegisterServiceCtrlHandlerEx function must be called before the first SetServiceStatus call because RegisterServiceCtrlHandlerEx returns a service status handle for the caller to use so that no other service can inadvertently set this service status. In addition, the control handler must be in place to receive control requests by the time the service specifies the controls it accepts through the SetServiceStatus function.

When the control handler function is invoked with a control request, the service must call SetServiceStatus to report status to the service control manager only if the service status has changed, such as when the service is processing stop or shutdown controls. If the service status has not changed, the service should not report status to the service control manager.

The service status handle does not have to be closed.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winsvc.h (include Windows.h)

Library

Advapi32.lib

DLL

Advapi32.dll

Unicode and ANSI names

RegisterServiceCtrlHandlerExW (Unicode) and RegisterServiceCtrlHandlerExA (ANSI)

See also

CreateService
HandlerEx
Service Control Handler Function
Service Functions
ServiceMain
SetServiceStatus

 

 

Send comments about this topic to Microsoft

Build date: 9/7/2011

Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ