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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值