StartService

startService启动过程分析 本文将开启对ActivityManagerService的分析。ActivityManagerService是Android的Java framework的服务框架最重要的服务之一。对于Andorid的Activity、Service、Broadcast、ContentProvider四剑客的管理,包含其生命周期都是通过ActivityManagerService来完成的。对于这四剑客的介绍,此处先略过,后续博主会针对这4剑客分别阐述。在整个startService过程,从进程角度看服务启动过程。 阅读详情

StartService

The StartService function starts a service.

BOOL StartService(
  SC_HANDLE hService,
  DWORD dwNumServiceArgs,
  LPCTSTR* lpServiceArgVectors
);
Parameters
hService
[in] Handle to the service. This handle is returned by the OpenService or CreateService function, and it must have the SERVICE_START access right. For more information, see Service Security and Access Rights.
dwNumServiceArgs
[in] Number of strings in the lpServiceArgVectors array. If lpServiceArgVectors is NULL, this parameter can be zero.
lpServiceArgVectors
[in] Pointer to an array of pointers to null-terminated strings to be passed to a service as arguments. Driver services do not receive these arguments. If no arguments are passed to the service, this parameter can be NULL. The service accesses these arguments through its ServiceMain function. The first argument (argv[0]) is the name of the service by default, followed by the arguments, if any, in the lpServiceArgVectors array.
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. Others 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 SERVICE_START access right.
ERROR_INVALID_HANDLEThe handle is invalid.
ERROR_PATH_NOT_FOUNDThe service binary file could not be found.
ERROR_SERVICE_ALREADY_RUNNINGAn instance of the service is already running.
ERROR_SERVICE_DATABASE_LOCKEDThe database is locked.
ERROR_SERVICE_DEPENDENCY_DELETEDThe service depends on a service that does not exist or has been marked for deletion.
ERROR_SERVICE_DEPENDENCY_FAILThe service depends on another service that has failed to start.
ERROR_SERVICE_DISABLEDThe service has been disabled.
ERROR_SERVICE_LOGON_FAILEDThe service did not start due to a logon failure. This error occurs if the service is configured to run under an account that does not have the "Log on as a service" right.
ERROR_SERVICE_MARKED_FOR_DELETEThe service has been marked for deletion.
ERROR_SERVICE_NO_THREADA thread could not be created for the service.
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.
Remarks

When a driver service is started, the StartService function does not return until the device driver has finished initializing.

When a service is started, the Service Control Manager (SCM) spawns the service process, if necessary. If the specified service shares a process with other services, the required process may already exist. The StartService function does not wait for the first status update from the new service, because it can take a while. Instead, it returns when the SCM receives notification from the service control dispatcher that the ServiceMain thread for this service was created successfully.

The SCM sets the following default status values before returning from StartService:

  • Current state of the service is set to SERVICE_START_PENDING.
  • Controls accepted is set to none (zero).
  • The CheckPoint value is set to zero.
  • The WaitHint time is set to 2 seconds.

The calling process can determine if the new service has finished its initialization by calling the QueryServiceStatus function periodically to query the service's status.

A service cannot call StartService during initialization. The reason is that the SCM locks the service control database during initialization, so a call to StartService will block. Once the service reports to the SCM that it has successfully started, it can call StartService.

As with ControlService, StartService 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, StartService fails with ERROR_SERVICE_REQUEST_TIMEOUT. This is because the SCM processes only one service control notification at a time.

Example Code

For an example, see Starting 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.
Unicode

Implemented as StartServiceW (Unicode) and StartServiceA (ANSI).

C++调用StartService启动服务失败1053分析与解决 网上通用的OpenSCManager->OpenService->StartService这种启动方式是正确的,但是很多人遇到开启服务时出现1053错误,下面来分析与解决 官网对于StartServiceW的描述(我程序里用的宽字符版本) https://docs.microsoft.com/zh-cn/windows/win32/api/winsvc/nf-winsvc-star... 阅读详情

相关推荐

TIMER-XL:突破长上下文限制的Transformer时序预测新范式

本文深入解析了清华大学提出的TIMER-XL模型,这是一种突破长上下文限制的Transformer时序预测新范式。它采用Decoder-only架构与补丁级生成技术,并创新性地引入TimeAttention机制,有效解决了传统模型在处理海量历史数据时面临的“上下文瓶颈”问题,显著提升了长序列预测的精度与稳定性。

weixin_28340315的博客 449

redis启动报错 HandleServiceCommands: system error caught. error code=1056, message = StartService failed

输入redis-server --service-start然后就会有如下报错。所以解决办法就是先将服务停止,然后再进行重新启动。

changyana的博客 7357

BLE天线设计避坑:NRF52832官方50欧姆阻抗匹配的3个常见误区

本文深入解析NRF52832蓝牙SoC天线设计中的常见误区,重点探讨50欧姆阻抗匹配的3个关键问题:PCB板材介电常数差异、匹配元件寄生参数及VSWR测试。通过实测数据和优化方案,帮助工程师规避设计陷阱,提升BLE通信性能。

beer8的博客 221

git 突然运行不了,phpstorm 提示git未安装,直接git bash here运行超时,解决方案之一

git 突然运行不了,phpstorm 提示git未安装,直接git bash here运行超时,解决方案之一

weixin_42968668的博客 1283

基于AIS动态数据与AI结合得经纬度标示算法

港口船舶智能标注系统 本系统通过结合摄像头视频流和AIS船舶动态数据,利用AI技术实现船舶实时标注功能。系统架构包括视频流处理、AIS数据解析、船舶检测匹配和坐标映射转换等模块。关键技术步骤: AIS数据获取:通过本地接收机或网络API获取船舶位置、航速等信息 AI目标检测:采用YOLOv8等模型识别视频中的船舶 船舶匹配算法:通过位置投影或运动轨迹匹配视频目标与AIS数据 坐标映射:使用相机标定技术将经纬度转换为图像坐标 实时标注:在视频画面上叠加船名、位置等关键信息 应用场景包括港口监控、智能海事管理和

qianbo042311的博客 1820

R 绘制环形树状图

按大类小类的从属关系绘制环形图,并把不同值的节点设置为相应大小。要求同一类的节点应为同一颜色。 按照输入,处理代码和输出记录

weixin_43718786的博客 1878

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

Windows服务编写原理及探讨(2)(二)对服务的深入讨论之上  上一章其实只是概括性的介绍,下面开始才是真正的细节所在。在进入点函数里面要完成ServiceMain的初始化,准确点说是初始化一个SERVICE_TABLE_ENTRY结构数组,这个结构记录了这个服务程序里面所包含的所有服务的名称和服务的进入点函数,下面是一个SERVICE_TABLE_ENTRY的例子:SERVICE_TABLE

神经网络爱好者 2077

Sigrity Power Tree如何进行直流压降仿真分析操作指导

Sigrity Power Tree模式可以用于前仿真,在没有layout文件的情况下对电源进行直流压降仿真分析,用于评估原理图设计以及器件选型是否合理。Model:设置为Equal Current,设置为等电源模型,意思该负载每个电源pin都是等电流的。Current:设置每个负载的电流,U30为3A,U3-U6各为1A。Nominal Current:设置能够承载的最大电流,设置为10A。Upper/ Lower Tol:设置为压降范围为5%Output Current:设置输出电流为10A。

weixin_54787054的博客 738

Windows无法启动启动服务,错误1053的解决方法

项目需求,要将一个exe二进制文件以windows服务的形式发布,开发编译运行后一切正常。sc命令安装为windows服务启动,出现下图错误Windows无法启动启动服务,错误1053。原因是加载配置文件的接口是用Getwd()获取的当前的工作目录,当将exe注册为windows服务运行时,Getwd获取的则是系统服务调用的工作目录。项目用的是golang开发,这里用到了os.Executable()来返回当前实际运行exe的路径,而不是调用者的工作路径。关键代码处打上crash日志。...

HonWee90的博客 2万+

基于四旋翼模型的火箭型无人机视线角保持控制仿真设计

本文针对火箭型无人机的视线角保持控制问题展开研究。基于四旋翼动力学模型,通过改造设计了一种摄像头朝上安装的特殊无人机系统,用于追踪空中目标。文章详细推导了坐标系定义、运动学和动力学方程,重点分析了摄像头安装与视线几何关系,建立了目标运动模型和视线向量计算方法。通过解算期望姿态角,实现了使机体Z轴始终对准目标的控制目标。仿真结果表明,该系统能有效完成视线角保持任务,为空对空监视等应用场景提供了可行方案。~~~~~

huanghm88的专栏 149

APP爬虫:Airtest的使用和爬取实战详解

Airtest是一个跨平台的自动化测试工具,主要用于移动应用和游戏的UI自动化测试。它支持图像识别、触摸操作、文本输入等功能,能够模拟真实用户的操作行为。此外,Airtest还具备强大的日志记录和报告生成功能,方便开发者进行调试和分析。图像识别:通过截图匹配元素,无需依赖 UI 层次结构。跨平台支持:支持 Android、iOS 和 Windows 应用。简单易用:提供 IDE 和脚本录制功能,降低使用门槛。

数据知道的博客 9630

Android startService和bindService的区别和用法

1、生命周期上的区别 执行startService时,Service会经历onCreate->onStartCommand。当执行stopService时,直接调用onDestroy方法。调用者如果没有stopService,Service会一直在后台运行,下次调用者再起来仍然可以stopService。 执行bindService时,Service会经历onCreate->onBind。这个时候调用者和Service绑定在一起。调用者调用unbindService方法或者调用者Context不

沧海龙腾LV的博客 2233

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

快速使用StartService

快速使用StartService 1.Service定义 Android四大组件之一 Service是一个可以在后台执行长时间运行操作而不提供用户界面的应用组件。 服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。 组件可以绑定到服务,以与之进行交互,甚至是执行进程间通信 (IPC) 2. 创建两个按钮: 启动按钮以及停止按钮 2.1 创建一个Service文件 2.2 编写server部分: package com.example.servicebasic; impor

qq_46258465的博客 2678

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

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

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

Android service 启动篇之 startService

Android 中service 详解中说明了大概背景,这里不再过叙述了。 代码基于Android O Android基础总结之六:Sevice中是应用端对于service 使用的总结,其中看到启动service 需要的接口有startService 和bindService。在Android O 中又添加了一个接口api——startForegroundService。本篇主要围...

私房菜 2万+

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

【Framework】startService启动流程

带你看Android 13下startService的实现原理

to_perfect的博客 1777

startService流程

开启Service进程的主要类 android.content包的ContextWrapper类 android.content包的Context类 android.app包的ContextImpl类 android.app包的ActivityManager类 android.os包的ServiceManager类 android.os包的ServiceManagerNative类 android...

fengluoye2012的博客 557

bindService和startService

服务 服务(service)是Android中实现程序后台运行的解决方案,它非常适合去执行那些不需要和用户交互而且还要求长期运行的任务。服务的运行不依赖于任何用户界面,即使程序被切换到后台,后者用户打开了另一个应用程序,服务仍然能够保持正常运行。 bindService bindService是绑定Service服务,执行service服务中逻辑流程。service通过Context.startS...

API搬运工 1337

bindService与startService

1. bindService既可以启动一个跨进程的service,也可以启动一个本进程内的service。     如果没有用aidl接口,则bindService不能bind另一个进程的service     如果使用了aidl接口,则在bindService()时,若service还没有启动,则系统会先启动service运行在的新进程。     当然,如果没用aidl,bindservi

hhitom的博客 3890

startService流程详解

1.Service整体交互结构 Service作为Android四大组件之一,其生命周期是通过system_server进程中的ActivityManagerService(AMS)管理的,大致了解Service通信过程中涉及到的几个主要角色。 App端进程: ContextImpl Context抽象类所有api的实现,是Service、Activity和其他组件base Context。 ActivityThread 代表着App的主线程,是App的入口,Application、Activ

尤教授的博客 3259

startService和bindService的区别

首先看下其生命周期的区别: 生命周期: onCreate → startCommand → onDestroy onCreate → onBind→onUnBind→ onDestroy 静态绑定对应着startService;动态绑定对应着bindService,静态有自己独立的生命周期,动态会依附activity等组件的生命周期。 1、生命周期上的区别 执行startService时,Service会经历onCreate->onStartCommand。当执行stopService时,直接调

梦否 5705

android service startService与bindService的区别

Android执行Service有两种方法,一种是startService,一种是bindService。下面让我们一起来聊一聊这两种执行Service方法的区别。 1、生命周期上的区别     执行startService时,Service会经历onCreate->onStartCommand。当执行stopService时,直接调用onDestroy方法。调用者如果没有st

zhuangyalei的专栏 8049

Android中bindService和startService启动服务有何区别

和是 Android 中两种用于与Service交互的方式,它们的区别主要在于和。

白泽的博客 1217

Android Service原理分析之startService(一)

1. Service概述 Service作为Android四大组件之一,在开发过程中非常常用,它虽然没有ui,但是同样可以在后台做很多重要的事情,我们平时使用启动service主要通过startService以及bindService来启动service以便已经相关操作。本文将介绍start...

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址