【原創】驅動加載之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 code Description
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM