131

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?

197

This can also be done via an elevated command prompt using the sc command. The syntax is:

sc config [service name] depend= <Dependencies(separated by / (forward slash))>

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 run depend= ServiceD, ServiceA will now depend only on ServiceD. (Thanks Matt!)

Examples

Dependency on one other service:

sc config ServiceA depend= ServiceB

Above means that ServiceA will not start until ServiceB has started. If you stop ServiceB, ServiceA will stop automatically.

Dependency on multiple other services:

sc config ServiceA depend= ServiceB/ServiceC/ServiceD/"Service Name With Spaces"

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:

sc config ServiceA depend= /

To list current dependencies:

sc qc ServiceA
  • 5
    If you want to see the existing dependencies before changing them with Kip's method, you can type sc qc YourServiceName – mivk Oct 1 '11 at 17:43
  • 1
    Just be careful... I just used this and didn't realize that it will overwrite existing dependencies.. there were two for my service that I don't remember.. now they're gone.. oh well! So make sure you run the command @mivk mentioned first to see any other dependencies first. – Matt Mar 28 '13 at 15:49
  • 1
    You can query currently configured dependency for a service by using sc qc [service name] command – gerrytan Aug 27 '13 at 6:57
  • 4
    If like me, your Service has spaces in the name, enclose it in quotes. – Lazlow Mar 18 '14 at 8:46
  • 6
    If using powershell, make sure you use sc.exe otherwise you will get error: 'A positional parameter cannot be found that accepts argument' – spuder Apr 17 '15 at 23:42
42

You can add service dependencies by adding the "DependOnService" value to the service in the registry using the regedit command, services can be found under HKEY_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:

To create a new dependency, select the subkey representing the service you want to delay, click Edit, and then click Add Value. Create a new value name "DependOnService" (without the quotation marks) with a data type of REG_MULTI_SZ, and then click OK. When the Data dialog box appears, type the name or names of the services that you prefer to start before this service with one entry for each line, and then click OK.

  • 6
    Holy Cow! That was the fastest answer I've ever received. I barely got back to the home page. – Rick Jun 12 '09 at 14:55
2

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:

Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\LanmanWorkstation' -Name DependOnService -Value @('Bowser','MRxSmb20','NSI')

Or, using WMI:

$DependsOn = @('Bowser','MRxSmb20','NSI','') #keep the empty array element at end
$svc = Get-WmiObject win32_Service -filter "Name='LanmanWorkstation'"
$svc.Change($null,$null,$null,$null,$null,$null,$null,$null,$null,$null,$DependsOn)

The Change method of the Win32_Service class helped point to a solution:

uint32 Change(
[in] string  DisplayName,
[in] string  PathName,
[in] uint32  ServiceType,
[in] uint32  ErrorControl,
[in] string  StartMode,
[in] boolean DesktopInteract,
[in] string  StartName,
[in] string  StartPassword,
[in] string  LoadOrderGroup,
[in] string  LoadOrderGroupDependencies[],
[in] string  ServiceDependencies[]
);
1

I wrote a simple .net application to manage service dependencies, if you are interested. It's free.

http://webpages.charter.net/bushman4/servicedependencymanager.html

  • 1
    It is telling me I need .NET 1.1 but I am running windows 7 – Nick Jun 17 '14 at 17:41
  • 1
    .Net 1.1 needs to be installed on Windows 7. See the answer to this question: answers.microsoft.com/en-us/windows/forum/… – Glenn Sullivan Jun 18 '14 at 18:29
  • 9
    Really, in 2014 you were recommending installing .NET 1.1 on Win7 instead of just recompiling it for 4.0? Thanks, no. – Endrju Jul 3 '15 at 21:43
0

In C++ (ATL) I did like this

bool ModifyDependOnService(void)
{
  CRegKey R;
  if (ERROR_SUCCESS == R.Open(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\services\\MyService"))
  {
    bool depIsThere = false;

    // determine if otherservice is installed, if yes, then add to dependency list.
    SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
    if (hSCManager)
    {
      SC_HANDLE hService = OpenService(hSCManager, L"OtherService", SERVICE_QUERY_STATUS);
      if (hService)
      {
        depIsThere = true;
        CloseServiceHandle(hService);
      }
      CloseServiceHandle(hSCManager);
    }

    std::wstring key = L"DependOnService";
    if (depIsThere )
    {
      const wchar_t deps[] = L"RPCSS\0OtherService\0";
      R.SetValue(key.c_str(), REG_MULTI_SZ, deps, sizeof(deps));
    }

    R.Close();
    return true;
  }
  return false;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.