【原创】驱动加载之StartService

  1. BOOL WINAPI StartService(
  2. _In_ SC_HANDLE hService,
  3. _In_ DWORD dwNumServiceArgs,
  4. _In_opt_ LPCTSTR *lpServiceArgVectors
  5. );
函数作用:开始一个服务
参数:
1. hService:服务句柄,由OpenService or CreateService取得,需要有SERVICE_START权限
2. dwNumServiceArgs:下一个形参lpServiceArgVectors的字符串个数,如果lpServiceArgVectors为空,那么该参数设为0
3. lpServiceArgVectors:传递给服务ServiceMain的参数,如果没有,可以设为NULL;否则,第一个形参lpServiceArgVectors[0]为服务的名字,其他则为需要传入的参数。
注意:驱动不接受这些参数,即lpServiceArgVectors为空,dwNumServiceArgs为0
 
返回值:
成功,非0;失败为0,错误码可以通过GetLastError获得;
Return codeDescription
ERROR_ACCESS_DENIED

The handle does not have the SERVICE_START access right.

ERROR_INVALID_HANDLE

The handle is invalid.

ERROR_PATH_NOT_FOUND

The service binary file could not be found.

ERROR_SERVICE_ALREADY_RUNNING

An instance of the service is already running.

ERROR_SERVICE_DATABASE_LOCKED

The database is locked.

ERROR_SERVICE_DEPENDENCY_DELETED

The service depends on a service that does not exist or has been marked for deletion.

ERROR_SERVICE_DEPENDENCY_FAIL

The service depends on another service that has failed to start.

ERROR_SERVICE_DISABLED

The service has been disabled.

ERROR_SERVICE_LOGON_FAILED

The 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_DELETE

The service has been marked for deletion.

ERROR_SERVICE_NO_THREAD

A thread could not be created for the service.

ERROR_SERVICE_REQUEST_TIMEOUT

The 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.

说明:
(1)当启动的是驱动服务,那么直到该驱动服务初始化完毕,StartService才返回
(2)如果必须的话,启动一个服务时SCM会产生一个服务进程。如果启动的服务和其他服务共享一个进程,那么所需要的进程可能已经存在,则SCM不需要再另外创建进程了。StartService不会等待更新自服务的第一个状态,因为这需要一段时间。然而,当SCM从服务控制调度程序那里接收到ServiceMain服务线程已经创建成功的通知,则StartService将会返回。
(3)在StartService返回前,SCM将会设置一下默认值:
  • 当前服务的状态设为SERVICE_START_PENDING
  • 接收的控制(Controls accepted)设为0
  • CheckPoint变量设为0
  • WaitHint时间值设为2s
(4)调用的程序能够定期地调用QueryServiceStatus函数获取服务的状态,判断新服务是否完成了初始化
(5)一个服务在初始化期间不能调用StartService,因为初始化期间,SCM会锁定SCM数据库,所以StartService会被暂停。当一个服务通知SCM已经初始化完成,那么该服务可以调用StartService
(6)和ControlService一样,如果其他服务正忙于处理控制码,StartService也有一个超时时间30s。如果忙于处理控制码的服务没有在30s内从它的处理函数返回,那么StartService将调用失败,返回ERROR_SERVICE_REQUEST_TIMEOUT,这是因为SCM一次只能处理一个服务控制通知。

本文链接:http://www.cnblogs.com/cposture/p/4717507.html

posted @ 2015-08-10 11:30  cposture  阅读(3819)  评论(0编辑  收藏  举报
levels of contents