RegisterServiceCtrlHandlerExA function (winsvc.h)
Registers a function to handle extended service control requests.
Syntax
SERVICE_STATUS_HANDLE RegisterServiceCtrlHandlerExA(
[in] LPCSTR lpServiceName,
[in] LPHANDLER_FUNCTION_EX lpHandlerProc,
[in, optional] LPVOID lpContext
);
Parameters
[in] lpServiceName
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.
[in] lpHandlerProc
A pointer to the handler function to be registered. For more information, see HandlerEx.
[in, optional] lpContext
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 code | Description |
---|---|
|
Not enough memory is available to convert an ANSI string parameter to Unicode. This error does not occur for Unicode string parameters. |
|
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.
Note
The winsvc.h header defines RegisterServiceCtrlHandlerEx as an alias that automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that is not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.
Requirements
Requirement | Value |
---|---|
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 |