I have a Windows Service that makes use of a SQL Server database. I don't have control over the installation of the service, but would like to add a dependency on the service to ensure that it starts after SQL server has started. (SQL server is running on the same machine as the service in question)
Is there a tool to add a dependency or possibly editing the registry directly?
This can also be done via an elevated command prompt using the
sc
command. The syntax is:Note: There is a space after the equals sign, and there is not one before it.
Warning:
depend=
parameter will overwrite existing dependencies list, not append. So for example, if ServiceA already depends on ServiceB and ServiceC, if you rundepend= ServiceD
, ServiceA will now depend only on ServiceD. (Thanks Matt!)Examples
Dependency on one other service:
Above means that ServiceA will not start until ServiceB has started. If you stop ServiceB, ServiceA will stop automatically.
Dependency on multiple other services:
Above means that ServiceA will not start until ServiceB, ServiceC, and ServiceD have all started. If you stop any of ServiceB, ServiceC, or ServiceD, ServiceA will stop automatically.
To remove all dependencies:
To list current dependencies:
You can add service dependencies by adding the "DependOnService" value to the service in the registry using the
regedit
command, services can be found underHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<Service name>
. The details can be found at MS KB article 193888, from which the following is an excerpt from:I was looking for a purely PowerShell (no regedit or sc.exe) method that can work on 2008R2/Win7 and newer, and came up with this:
Easy one is do the regedit with PowerShell:
Or, using WMI:
The Change method of the Win32_Service class helped point to a solution:
I wrote a simple .net application to manage service dependencies, if you are interested. It's free.
http://webpages.charter.net/bushman4/servicedependencymanager.html
In C++ (ATL) I did like this