標籤:
BOOL WINAPI ControlService(
_In_ SC_HANDLE hService,
_In_ DWORD dwControl,
_Out_ LPSERVICE_STATUS lpServiceStatus
);
函數作用:給指定的服務發送一個控制碼參數:1. hService: OpenService 或CreateService function返回的服務控制代碼;2. dwControl:要發送的控制碼,控制碼可以是下列系統定義的控制碼;也可以是使用者自訂的控制碼,範圍是128-255,需要有SERVICE_USER_DEFINED_CONTROL許可權。
| Control code |
Meaning |
-
SERVICE_CONTROL_CONTINUE
-
0x00000003
|
Notifies a paused service that it should resume. The hService handle must have the SERVICE_PAUSE_CONTINUE access right.
(恢複服務,需要SERVICE_PAUSE_CONTINUE許可權) |
-
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.
Note that this control is not generally useful as the SCM is aware of the current state of the service.
(向SCM報表服務的目前狀態資訊,需要有SERVICE_INTERROGATE許可權) |
-
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.
(網路綁定相關,已廢棄) |
-
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.
(網路綁定相關,已廢棄) |
-
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.
(網路綁定相關,已廢棄) |
-
SERVICE_CONTROL_NETBINDREMOVE
-
0x00000008
|
Notifies a network service 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.
(網路綁定相關,已廢棄) |
-
SERVICE_CONTROL_PARAMCHANGE
-
0x00000006
|
Notifies a service that its startup parameters have changed. The hService handle must have the SERVICE_PAUSE_CONTINUE access right.
(服務啟動參數已經改變,需要有SERVICE_PAUSE_CONTINUE許可權) |
-
SERVICE_CONTROL_PAUSE
-
0x00000002
|
Notifies a service that it should pause. The hService handle must have the SERVICE_PAUSE_CONTINUE access right.
(暫停服務,需要有SERVICE_PAUSE_CONTINUE許可權) |
-
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.
(停止服務,需要有SERVICE_STOP許可權) |
3. lpServiceStatus:傳回值,一個指向儲存服務最新狀態的結構體ServiceStatus,返回的服務資訊來自SCM中最近的服務狀態報表。只有當ControlService返回以下的錯誤碼:NO_ERROR, ERROR_INVALID_SERVICE_CONTROL, ERROR_SERVICE_CANNOT_ACCEPT_CTRL, or ERROR_SERVICE_NOT_ACTIVE,SCM才會填充結構體ServiceStatus。 傳回值:成功,返回非0;失敗,返回0,錯誤碼使用GetLastError獲得;
| Return code |
Description |
-
ERROR_ACCESS_DENIED
|
The handle does not have the required access right. |
-
ERROR_DEPENDENT_SERVICES_RUNNING
|
The service cannot be stopped because other running services are dependent on it. |
-
ERROR_INVALID_HANDLE
|
The specified handle was not obtained using CreateService or OpenService, or the handle is no longer valid. |
-
ERROR_INVALID_PARAMETER
|
The requested control code is undefined. |
-
ERROR_INVALID_SERVICE_CONTROL
|
The requested control code is not valid, or it is unacceptable to the service. |
-
ERROR_SERVICE_CANNOT_ACCEPT_CTRL
|
The 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_ACTIVE
|
The service has not been started. |
-
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. |
-
ERROR_SHUTDOWN_IN_PROGRESS
|
The system is shutting down. |
說明:(1)ControlService函數要求服務控制管理員(SCM)向服務發送請求的控制碼。SCM發送的前提是服務已經指定要接收控制碼,或服務處於控制碼可以發送給它的一個狀態。(2)SCM是以串列的方式處理服務控制通知——它會等待服務完成一個服務控制通知的處理,才發送下一個通知。所以如果任何服務正忙於處理一個控制碼,ControlService的一個調用將會暫停30s的時間(這是一個逾時時間),如果逾時時間一過但處理還沒完成,那麼函數將返回一個ERROR_SERVICE_REQUEST_TIMEOUT。(3)其他:
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 state |
Stop |
Other 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.
本文連結:http://www.cnblogs.com/cposture/p/4717546.html
【原創】驅動卸載之ControlService函數