RegisterServiceCtrlHandlerEx

博客介绍了RegisterServiceCtrlHandlerEx函数,该函数用于服务注册处理服务控制请求的函数,取代了RegisterServiceCtrlHandler函数。文中说明了其参数、返回值,强调在首次调用SetServiceStatus前调用此函数,控制处理函数被调用时需通知服务控制管理器状态。

RegisterServiceCtrlHandlerEx

A service calls the RegisterServiceCtrlHandlerEx function to register a function to handle its service control requests.

This function supersedes the RegisterServiceCtrlHandler 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.

SERVICE_STATUS_HANDLE RegisterServiceCtrlHandlerEx(
  LPCTSTR lpServiceName,
  LPHANDLER_FUNCTION_EX lpHandlerProc,
  LPVOID lpContext
);
Parameters
lpServiceName
[in] Pointer to a null-terminated string that specifies 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] Pointer to the handler function to be registered. For more information, see HandlerEx.
lpContext
[in] User-defined data. This parameter, which is passed to the handler function, can help identify the service when multiple services share a process.
Return Values

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. Other error codes can be set by the registry functions that are called by the service control manager.

Return codeDescription
ERROR_INVALID_NAMEThe specified service name is invalid.
ERROR_SERVICE_DOES_NOT_EXISTThe specified service does not exist.
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. 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, it must call SetServiceStatus to notify the service control manager of its current status, regardless of whether the status of the service has changed.

The service status handle does not have to be closed.

Requirements
ClientRequires Windows XP or Windows 2000 Professional.
ServerRequires Windows Server 2003 or Windows 2000 Server.
Header

Declared in Winsvc.h; include Windows.h.

Library

Link to Advapi32.lib.

DLLRequires Advapi32.dll.
Unicode

Implemented as RegisterServiceCtrlHandlerExW (Unicode) and RegisterServiceCtrlHandlerExA (ANSI).

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值