ControlService

Windows 编程之服务-2 四、自定义服务控制码 除了系统定义的五种控制码外(还有一种是:SERVICE_CONTROL_SHUTDOWN), 用户还可自定义控制码,其取值范围是128-255。用户可以通过控制面板中的服务 项向特定服务程序的控制处理函数发送控制码,程序员可以调用系统函数  ControlService 直接向服务程序的控制处理函数发送控制码。其函数原型如下: BOOL ControlServi 阅读详情

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.
[Win32] SCManager 服务控制管理器API(1) 1。OpenSCManager 打开服务控制管理器 函数原型: SC_HANDLE WINAPI OpenSCManager( _In_opt_ LPCTSTR lpMachineName, _In_opt_ LPCTSTR lpDatabaseName, _In_ DWORD dwDesiredAccess );lpMachineName:置NULL lpD 阅读详情

相关推荐

C# 驱动卸载 / UniSysDriver

如果你需要了解驱动如何加载,那么可以参考我的这篇文章 C# 驱动加载 / LoadSysDriver 总体两者的方法差异不大 public static bool UniSysDriver(string strServiceName) { IntPtr hSCManager = OpenSCManager(null, null, SC_MA

987

【原创】驱动卸载之ControlService函数

BOOL WINAPI ControlService( _In_ SC_HANDLE hService, _In_ DWORD dwControl, _Out_ LPSERVICE_STATUS lpServiceStatus ); 函数作用:给指定的服务发送一个控制码 参数: 1. hService: OpenService 或CreateService function返...

weixin_34054866的博客 268

AniME框架深度体验:用AI自动生成《修真小说》动画全流程记录

本文详细记录了使用开源多智能体框架AniME,结合其核心动画生成模型AniSora V3,将一段修真小说文本自动转化为风格统一动画视频的全流程实战。文章深入解析了从环境搭建、导演智能体任务分解,到多智能体协作生成分镜、角色、动画及最终合成的每一步操作与挑战,为AI驱动的长视频创作提供了宝贵的实践经验与流程优化思路。

ol789012345的博客 626

Windows下的服务控制管理器(SCM)

一、概要           服务管理器是用于管理系统服务的管理工具。一般常用于windows系统,使用这个工具你可以启动、停止服务;设置服务是自动、手动启动或禁用;查看某个服务的相关信息;设置服务以什么用户启动等等(一般包括了超级管理员用户、管理员用户和游客用户);设置用户注销,软件等。

xcbeyond|疯狂源自梦想,技术成就辉煌 2万+

MOSFET导通电阻Rdson全解析:物理组成、公式推导与工程选型要点

在功率电子设计中,MOSFET作为核心开关器件,其导通电阻Rdson直接决定导通损耗与系统效率。从半导体物理角度看,Rdson并非单一固定值,而是由源极电阻、沟道电阻、累积层电阻、JFET区电阻、漂移区电阻及衬底电阻等多个分量串联而成,并随栅极驱动电压、结温以及击穿电压等级发生显著变化。理解Rdson的物理来源与数学表达,有助于工程师在低压大电流与高压小电流等不同场景中合理选型、优化驱动电压并准确估算热损耗。文章从沟道电阻公式出发,深入剖析耐压与导通电阻的物理矛盾、温度系数的影响规律,并给出数据手册参数的正

weixin_29163857的博客 198

Oracle EBS R12 AR 标准发票 核心表结构

​ Oracle EBS R12 AR 标准发票 核心表结构 聚焦手动标准发票 (Standard Invoice),按头→行→分配→税→收款计划→会计分录→历史分层,标注主键、外键、关键字段、业务含义,同时说明表间关联关系,适配开发 / 运维 / 数据核对场景。 一、主数据层(发票头 + 行,核心业务表) 1. RA_CUSTOMER_TRX_ALL 【发票头表】 作用:存储整张发票头部信息,一张发票对应一条记录,AR 发票主表。 主键:CUSTOMER_TRX_ID (唯一 ID,系统内部键) 业

金牌架构师 285

关于C++对 service 的控制

最简单的方式  ShellExecute( "net   stop   servicename ",...);  ShellExecute( "net   start   servicename ",...);  复杂的就是API函数  SC_HANDLE   OpenSCManager(      LPCTSTR   lpMachineName,       //   computer

xiunai78的专栏 1270

SAP会计凭证导入ABAP开发模板+注意点—附带源码

开发并不仅仅只要考虑实现的功能,只实现功能的是程序,考虑到使用场景的是工程。使用bapi校验创建会计凭证的数据,报错和参数和正式创建一致。用途,防止产生错误的凭证,即使财务凭证可以冲销。正式创建会计凭证。隐藏的坑。

小九啥也不会 1233

windows服务程序二

Windows 服务程序(二) </h1> <div class="clear"></div> <div class="postBody"> <div id="cnblogs_post_body" class="blogpost-body blogpost-body-html"> 服务...

宇之日记 621

点云统计滤波 (python详细过程版)【2025最新版】

统计滤波python详细过程版,博客长期更新,本文最近更新时间为:2025年1月12日。

点云侠的博客 5639

〔重庆理工大学〕大学物理实验--变温霍耳效应及其参数测定

每个老师要求不同,实验报告内容和数据仅供参考。

Pretty Boy Fox的博客 3044

Windows服务编写原理及探讨(四)

(四)一些问题的讨论   前面几章的内容都是服务的一些通用的编写原理,但里面隐含着一些问题,编写简单的服务时看不出来,但遇到复杂的应用就会出现一些问题,所以本章就是用来分析、解决这些问题的,适用于高级应用的开发人员。我这一章的内容都是经过实验得到的,很有实际意义。   我在第一章里面就说过,是由一个服务的主线程执行CtrlHandler函数,它将收到各种控制命令,但是真正处理命令,执行操作的是Se

hbb0b0的专栏 1051

webrtc进阶-- FlexFEC 实现原理与调试分析(m80 代码对齐版)

WebRTC 中 FlexFEC 技术解析与调试指南 摘要:本文深入分析 WebRTC 的 FlexFEC(灵活前向纠错)技术实现原理,重点探讨其在 m80 版本中的协商机制、接收侧恢复流程以及 SFU 场景下的特殊处理。文章指出 FlexFEC 通过动态冗余策略提升弱网抗丢包能力,详细解析了编解码器分配、SSRC 映射、包恢复等关键环节,并提供了针对 XR+SFU 场景的调试建议。调试时可重点关注 FEC 包接收与媒体包恢复日志,以验证链路有效性。

HOSTEN的博客 49

stf源码解读之ControlService-sendTwoWay(socket.io)双向通信

文章目录前言touchDown与installsendOneWay与sendTwoWay疑点TransactionService后台的消息发哪去了 前言 本文主要解读ControlService的sendTwoWay和sendOneWay的区别,sendTwoWay函数怎么实现双向通信的。 touchDown与install 我们看到 res/app/components/stf/screen/screen-directive.js有这样的调用方式 control.touchDown(nextSeq(),

harrain的博客 635

【STM32Cube_14】使用硬件I2C读写环境光强度传感器(BH1750)

本篇详细的记录了如何使用STM32CubeMX配置STM32L431RCT6的硬件I2C外设读取环境光强度传感器数据(BH1750)

Mculover666的博客(嵌入式) 7243

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

[病毒分析]WinDBG实战教学(1)

WinDBG实战教学(1)一、初始任务二、开始分析三、分析代码四、WinDBG调试 这里的实例选自《恶意代码分析与实战》第十章实验一 大家可以在这里下载哦 点击下载—提取码:8189 实验工具: 1、WinDBG 2、IDA 3、Dependency Walker 一、初始任务 我们将下载好的文件放到指定位置 C:\Windows\System32 使用WinGDB 连接至虚拟机(优快云中有很多关于WinDBG的初始教学,大家可以先看看) 出现下面的界面表示连接成功: 二、开始分析 第一步,我们使用D

网络安全知识分享 4145

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

c++ 驱动【加载,启动,停止,卸载】

驱动代码参考:https://blog.youkuaiyun.com/Simon798/article/details/103308039 exe 代码: #include <iostream> #include <Windows.h> using namespace std; // 安装驱动 BOOL installDvr(CONST WCHAR drvPath[50], CONST WCHAR serviceName[20]) { // 打开服务控制管理器数据库

LYSM 6079

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Android Studio求解Android SDK is up to date. Running Android Emulator hypervisor driver installer

由于发生错误 4294967201, StartService 失败。[SC] DeleteService 成功。看不懂错误是啥意思啊。

qq_51463650的博客 943

SERVICE_STATUS

SERVICE_STATUSThe SERVICE_STATUS structure contains information about a service. The ControlService, EnumDependentServices, EnumServicesStatus, and QueryServiceStatus functions use this structure to r

小发猫 2434

Windows XP下Service的编程入门[1]

Windows XP下Service的编程入门[1]// _dse.cpp : Defines the entry point for the console application.// 作者: 秋镇菜// E-mail: cyin8@msn.com#include "stdafx.h"#include "windows.h"int main(int argc, char* argv[]){ L

小发猫 1730

用delphi如何实现启动停止windows服务

function StartService(AServName: string): Boolean; //use WinSvc var SCManager, hService: SC_HANDLE; lpServiceArgVectors: PChar; begin SCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCE...

200

NT式驱动的卸载

NT式驱动的卸载 A、卸载驱动流程 B、内核函数DeleteService C、内核函数ControlService D、构建UnLoadSys函数 E、测试并查看调试信息 一、卸载驱动流程 1、用OpenSCManager函数打开 服务控制管理器,取得SCM句柄,如果返回NULL,则结束,否则继续2.//hSCM 2、用OpenService打开相应的服务,如果返回NULL,则结束,否则继续3.hSerVice 3、用ControlService停止驱动服务,如果返回NULL,只有重新启动才能,再动态加载

Windows内核学习笔记 298

服务编程——给一个服务发控制码

1、OpenSCManager 2、OpenService 3、Con

心之所向,身之所往 657

注册和卸载windows服务进程

注册和卸载windows服务进程

tangaowen的专栏 4103
上一篇: ChangeServiceConfig
下一篇: ChangeServiceConfig2
普通网友
博客等级 码龄22年 1万+粉丝 3156原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值