Skip to content

Latest commit

 

History

History
307 lines (237 loc) · 8.7 KB

nf-winsvc-startservicew.md

File metadata and controls

307 lines (237 loc) · 8.7 KB
UID title description helpviewer_keywords old-location tech.root ms.assetid ms.date ms.keywords req.header req.include-header req.target-type req.target-min-winverclnt req.target-min-winversvr req.kmdf-ver req.umdf-ver req.ddi-compliance req.unicode-ansi req.idl req.max-support req.namespace req.assembly req.type-library req.lib req.dll req.irql targetos req.typenames req.redist ms.custom f1_keywords dev_langs topic_type api_type api_location api_name
NF:winsvc.StartServiceW
StartServiceW function (winsvc.h)
Starts a service. (Unicode)
StartService
StartService function
StartServiceW
_win32_startservice
base.startservice
winsvc/StartService
winsvc/StartServiceW
base\startservice.htm
security
f185a878-e1c3-4fe5-8ec9-c5296d27f985
12/05/2018
StartService, StartService function, StartServiceA, StartServiceW, _win32_startservice, base.startservice, winsvc/StartService, winsvc/StartServiceA, winsvc/StartServiceW
winsvc.h
Windows.h
Windows
Windows XP [desktop apps only]
Windows Server 2003 [desktop apps only]
StartServiceW (Unicode) and StartServiceA (ANSI)
Advapi32.lib
Advapi32.dll
Windows
19H1
StartServiceW
winsvc/StartServiceW
c++
APIRef
kbSyntax
DllExport
Advapi32.dll
API-MS-Win-DownLevel-AdvApi32-l2-1-1.dll
sechost.dll
API-MS-Win-Service-management-l1-1-0.dll
API-MS-Win-Service-Winsvc-l1-1-0.dll
API-MS-Win-Service-Winsvc-l1-2-0.dll
StartService
StartServiceA
StartServiceW

StartServiceW function

-description

Starts a service.

-parameters

-param hService [in]

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

-param dwNumServiceArgs [in]

The number of strings in the lpServiceArgVectors array. If lpServiceArgVectors is NULL, this parameter can be zero.

-param lpServiceArgVectors [in, optional]

The null-terminated strings to be passed to the ServiceMain function for the service as arguments. If there are no arguments, this parameter can be NULL. Otherwise, the first argument (lpServiceArgVectors[0]) is the name of the service, followed by any additional arguments (lpServiceArgVectors[1] through lpServiceArgVectors[dwNumServiceArgs-1]).

Driver services do not receive these arguments.

-returns

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

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

Examples

For an example, see Starting a Service.

Note

The winsvc.h header defines StartService as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.

-see-also

ControlService

CreateService

DeleteService

OpenService

QueryServiceDynamicInformation

QueryServiceStatusEx

Service Functions

Service Startup

ServiceMain