Export (0) Print
Expand All

RegisterServiceCtrlHandler function

Registers a function to handle service control requests.

This function has been superseded by the RegisterServiceCtrlHandlerEx function. A service can use either function, but the new function supports user-defined context data, and the new handler function supports additional extended control codes.

Syntax


SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandler(
  _In_  LPCTSTR lpServiceName,
  _In_  LPHANDLER_FUNCTION lpHandlerProc
);

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.

If the service type is SERVICE_WIN32_OWN_PROCESS, the function does not verify that the specified name is valid, because there is only one registered service in the process.

lpHandlerProc [in]

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

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 RegisterServiceCtrlHandler 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 Handler. 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 RegisterServiceCtrlHandler function must be called before the first SetServiceStatus call because RegisterServiceCtrlHandler 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.

Examples

For an example, see Writing a ServiceMain Function.

Requirements

Minimum supported client

Windows XP [desktop apps only]

Minimum supported server

Windows Server 2003 [desktop apps only]

Header

Winsvc.h (include Windows.h)

Library

Advapi32.lib

DLL

Advapi32.dll

Unicode and ANSI names

RegisterServiceCtrlHandlerW (Unicode) and RegisterServiceCtrlHandlerA (ANSI)

See also

CreateService
Handler
RegisterServiceCtrlHandlerEx
Service Control Handler Function
Service Functions
ServiceMain
SetServiceStatus

 

 

Community Additions

ADD
Show:
© 2015 Microsoft