StopService method of the Win32_Service Class
Applies to: desktop apps only
The StopService WMI class method places the service, represented by the Win32_Service object, in the stopped state.
This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.
Syntax
uint32 StopService();
Parameters
This method has no parameters.
Return value
| Return code | Description |
|---|---|
|
Success |
|
Not Supported |
|
Access Denied |
|
Dependent Services Running |
|
Invalid Service Control |
|
Service Cannot Accept Control |
|
Service Not Active |
|
Service Request timeout |
|
Unknown Failure |
|
Path Not Found |
|
Service Already Stopped |
|
Service Database Locked |
|
Service Dependency Deleted |
|
Service Dependency Failure |
|
Service Disabled |
|
Service Logon Failed |
|
Service Marked For Deletion |
|
Service No Thread |
|
Status Circular Dependency |
|
Status Duplicate Name |
|
Status - Invalid Name |
|
Status - Invalid Parameter |
|
Status - Invalid Service Account |
|
Status - Service Exists |
|
Service Already Paused |
Remarks
If you attempt to stop a service which has dependent services running, the StopService method fails with a return value of 3. The dependent services must be stopped first.
If you stop a service, then immediately check the Win32_Service.State property, as the value may still show the service as running.
Examples
For script code examples, see WMI Tasks for Scripts and Applications and the TechNet ScriptCenter Script Repository.
For C++ code examples, see WMI C++ Application Examples.
The following VBScript code example shows that you cannot stop the NetDDE service until the dependent services have been stopped. To run the script, ensure that the NetDDE service and its dependent services are running by using the Services.msc MMC snap-in or the Net Start command.
The Win32_DependentService class allows you to locate service dependencies through an Associators Of query.
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2") Set objNetDDEservice = _ objWMIService.Get("Win32_Service.Name='NetDDE'") WScript.Echo "NetDDE service state: " & objNetDDEService.State WScript.Echo "Stopping NetDDE service" Return = objNetDDEService.StopService() WScript.Echo "Return value: " & Return & _ " Service cannot be stopped because " & _ "dependent services are running" Set colServiceList = objWMIService.ExecQuery("Associators of " _ & "{Win32_Service.Name='NetDDE'} Where " _ & "AssocClass=Win32_DependentService " & _ "Role=Antecedent" ) For Each objService in colServiceList WScript.Echo "Dependent service: " & objService.Name & _ " State: " & objService.State WScript.Echo "Stopping dependent service " & objService.Name objService.StopService() Next Wscript.Sleep 20000 WScript.Echo "Stopping NetDDE service" Return = objNetDDEService.StopService() WScript.Echo "Return value: " & Return
Requirements
|
Minimum supported client | Windows 2000 Professional |
|---|---|
|
Minimum supported server | Windows 2000 Server |
|
Namespace |
\root\CIMV2 |
|
MOF |
|
|
DLL |
|
See also
Send comments about this topic to Microsoft
Build date: 3/9/2012