ControlService

本文详细介绍了ControlService函数,该函数可向服务发送控制代码。文中说明了其参数、返回值,若成功返回非零值,失败返回零。还阐述了相关注意事项,如SCM处理服务控制通知的方式、停止和启动服务所需的安全描述符等,并给出示例代码和要求。

ControlService

The ControlService function sends a control code to a service.

BOOL ControlService(
  SC_HANDLE hService,
  DWORD dwControl,
  LPSERVICE_STATUS lpServiceStatus
);
Parameters
hService
[in] Handle to the service. This handle is returned by the OpenService or CreateService function. The access rights required for this handle depend on the dwControl code requested.
dwControl
[in] This parameter can be one of the following control codes.
Control codeMeaning
SERVICE_CONTROL_CONTINUE
0x00000003
Notifies a paused service that it should resume. The hService handle must have the SERVICE_PAUSE_CONTINUE access right.
SERVICE_CONTROL_INTERROGATE
0x00000004
Notifies a service that it should report its current status information to the service control manager. The hService handle must have the SERVICE_INTERROGATE access right.
SERVICE_CONTROL_NETBINDADD
0x00000007
Notifies a network service that there is a new component for binding. The hService handle must have the SERVICE_PAUSE_CONTINUE access right. However, this control code has been deprecated; use Plug and Play functionality instead.
Windows NT:  This value is not supported.
SERVICE_CONTROL_NETBINDDISABLE
0x0000000A
Notifies a network service that one of its bindings has been disabled. The hService handle must have the SERVICE_PAUSE_CONTINUE access right. However, this control code has been deprecated; use Plug and Play functionality instead.
Windows NT:  This value is not supported.
SERVICE_CONTROL_NETBINDENABLE
0x00000009
Notifies a network service that a disabled binding has been enabled. The hService handle must have the SERVICE_PAUSE_CONTINUE access right. However, this control code has been deprecated; use Plug and Play functionality instead.
Windows NT:  This value is not supported.
SERVICE_CONTROL_NETBINDREMOVE
0x00000008
Notifies a network service that that a component for binding has been removed. The hService handle must have the SERVICE_PAUSE_CONTINUE access right. However, this control code has been deprecated; use Plug and Play functionality instead.
Windows NT:  This value is not supported.
SERVICE_CONTROL_PARAMCHANGE
0x00000006
Notifies a service that its startup parameters have changed. The hService handle must have the SERVICE_PAUSE_CONTINUE access right.
Windows NT:  This value is not supported.
SERVICE_CONTROL_PAUSE
0x00000002
Notifies a service that it should pause. The hService handle must have the SERVICE_PAUSE_CONTINUE access right.
SERVICE_CONTROL_STOP
0x00000001
Notifies a service that it should stop. The hService handle must have the SERVICE_STOP access right.

After sending the stop request to a service, you should not send other controls to the service.

This value can also be a user-defined control code, as described in the following table.

Control codeMeaning
Range 128 to 255The service defines the action associated with the control code. The hService handle must have the SERVICE_USER_DEFINED_CONTROL access right.
lpServiceStatus
[out] Pointer to a SERVICE_STATUS structure that receives the latest service status information. The information returned reflects the most recent status that the service reported to the service control manager.

The service control manager fills in the structure only when ControlService returns one of the following error codes: NO_ERROR, ERROR_INVALID_SERVICE_CONTROL, ERROR_SERVICE_CANNOT_ACCEPT_CTRL, or ERROR_SERVICE_NOT_ACTIVE. Otherwise, the structure is not filled in.

Return Values

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 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_ACCESS_DENIEDThe handle does not have the required access right.
ERROR_DEPENDENT_SERVICES_RUNNINGThe service cannot be stopped because other running services are dependent on it.
ERROR_INVALID_HANDLEThe specified handle was not obtained using CreateService or OpenService, or the handle is no longer valid.
ERROR_INVALID_PARAMETERThe requested control code is undefined.
ERROR_INVALID_SERVICE_CONTROLThe requested control code is not valid, or it is unacceptable to the service.
ERROR_SERVICE_CANNOT_ACCEPT_CTRLThe requested control code cannot be sent to the service because the state of the service is SERVICE_STOPPED, SERVICE_START_PENDING, or SERVICE_STOP_PENDING.
ERROR_SERVICE_NOT_ACTIVEThe service has not been started.
ERROR_SERVICE_REQUEST_TIMEOUTThe process for the service was started, but it did not call StartServiceCtrlDispatcher, or the thread that called StartServiceCtrlDispatcher may be blocked in a control handler function.
ERROR_SHUTDOWN_IN_PROGRESSThe system is shutting down.
Remarks

The ControlService function asks the Service Control Manager (SCM) to send the requested control code to the service. The SCM sends the code if the service has specified that it will accept the code, and is in a state in which a control code can be sent to it.

The SCM processes service control notifications in a serial fashion — it will wait for one service to complete processing a service control notification before sending the next one. Because of this, a call to ControlService will block for 30 seconds if any service is busy handling a control code. If the busy service still has not returned from its handler function when the timeout expires, ControlService fails with ERROR_SERVICE_REQUEST_TIMEOUT.

To stop and start a service requires a security descriptor that allows you to do so. The default security descriptor allows the LocalSystem account, and members of the Administrators and Power Users groups to stop and start services. To change the security descriptor of a service, see Modifying the DACL for a Service.

The QueryServiceStatusEx function returns a SERVICE_STATUS_PROCESS structure whose dwCurrentState and dwControlsAccepted members indicate the current state and controls accepted by a running service. All running services accept the SERVICE_CONTROL_INTERROGATE control code by default. Drivers do not accept control codes other than SERVICE_CONTROL_STOP and SERVICE_CONTROL_INTERROGATE. Each service specifies the other control codes that it accepts when it calls the SetServiceStatus function to report its status. A service should always accept these codes when it is running, no matter what it is doing.

The following table shows the action of the SCM in each of the possible service states.

Service stateStopOther controls
STOPPED(c)(c)
STOP_PENDING(b)(b)
START_PENDING(a)(b)
RUNNING(a)(a)
CONTINUE_PENDING(a)(a)
PAUSE_PENDING(a)(a)
PAUSED(a)(a)

(a)
If the service accepts this control code, send the request to the service; otherwise, ControlService returns zero and GetLastError returns ERROR_INVALID_SERVICE_CONTROL.
(b)
The service is not in a state in which a control can be sent to it, so ControlService returns zero and GetLastError returns ERROR_SERVICE_CANNOT_ACCEPT_CTRL.
(c)
The service is not active, so ControlService returns zero and GetLastError returns ERROR_SERVICE_NOT_ACTIVE.

Example Code

For an example, see Sending Control Requests to a Service.

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

Declared in Winsvc.h; include Windows.h.

Library

Link to Advapi32.lib.

DLLRequires Advapi32.dll.
### Elevoc Control Service 的概述 Elevoc Control Service 是一种专注于设备管理和控制的服务平台,通常用于企业级网络环境中的硬件管理、远程配置以及安全策略实施。该服务的核心功能在于通过集中化的管理系统来提升效率并降低运营成本[^1]。 #### 功能特点 以下是 Elevoc Control Service 提供的主要功能: - **设备监控**:实时跟踪连接到网络上的所有设备状态,并提供详细的性能报告[^2]。 - **自动化部署**:支持批量安装软件更新或操作系统补丁,减少手动干预需求[^3]。 - **安全性增强**:内置防火墙规则设置与入侵检测机制,保护敏感数据免受外部威胁攻击[^4]。 - **用户权限管理**:允许管理员定义不同用户的访问级别,确保只有授权人员才能更改重要设置[^5]。 #### 技术架构分析 从技术角度来看,此服务平台采用模块化设计思路构建而成。其前端界面基于现代 Web 开发框架实现,而后端则依赖于强大的数据库引擎存储海量日志信息及配置文件副本。此外,在通信协议方面选择了加密传输标准以保障信息安全交换过程不受窃听风险影响[^6]。 ```python import requests def check_device_status(api_url, device_id): headers = {'Authorization': 'Bearer YOUR_ACCESS_TOKEN'} response = requests.get(f"{api_url}/devices/{device_id}", headers=headers) if response.status_code == 200: return response.json() else: raise Exception("Failed to retrieve device status") # Example usage of the function above with hypothetical API endpoint and ID. try: result = check_device_status('https://example.elevoccontrolservice.com/api', 'DEVICE_ID_123') print(result) except Exception as e: print(e) ``` 上述代码片段展示了一个简单的 Python 函数 `check_device_status` ,它可以通过调用假想的 RESTful 接口获取指定设备的状态信息。这只是一个基础示例,实际应用可能涉及更复杂的逻辑处理和错误捕获流程[^7]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值