views:

209

answers:

2

Hey folks

I've recently written a Windows service and installed it on the production system. After some applied Windows updates, the server had to be rebooted and my service did not load correctly, although it was set to start automatically. The service started fine manually after we were noticed of the failure by customers. After some research, I found out that I missed to add a service dependency to our service. So the boot order of the services was wrong for our case.

I could fix the issue, but I'm now really interested in a tool or procedure on how to collect all necessary service dependencies. Do you know any piece of code which can handle this?

I appreciate any hint.

+1  A: 

How would a tool know which services you need? By analyzing the source code? By running your service until the service fails?

The only way to make this work is to know the services on which you depend and add them to the registry when you install your service.

I'm not saying that it would be impossible to automate this; but if it could be automated, then Windows would already be doing it: When connecting to another service, Windows could stop your startup until that other service is available.

Aaron Digulla
Analyzing the source code would be an option. Let's say you use i.e. `ManagementObjectSearcher`, a class which certainly uses WMI. So add the dependency. Maybe it would also be possible based on the assembly dependencies, although this would also add false positives.
Scoregraphic
I still think it would be more simple if Windows would figure this out itself when it starts a service. It's not rocket science.
Aaron Digulla
+1  A: 

Services and their dependencies are described in the Registry under HKLM/System/CurrentControlSet/Services/xxx where xxx is the name of a service. There are hundreds of services and only some of them are visible in the Services.msc console. In the services console, you can bring up the property window for a service and look at the Dependencies tab. There will be enumerated the services that depend on the service as well as those that the service depends on. In the registry each service has two optional keys named "DependOnService" and "DependOnGroup". Both are of type REG_MULTI_SZ which means they can contain multiple values. Use RegEdt32.exe when looking at these values. This is where dependencies are defined. If you want to make your service dependent on Microsoft SQL Server for example, in the key for your service, add a key named "DependOnService" containing "MSSQLSERVER". Verify this by looking at the Dependencies tab of your service properties.

If you want to discover service dependencies, you need to programmatically walk the Services key in the registry noting the services and the services they depend on. Once you've done that you can just print out the results.

I wrote something similar to this when I wanted to discover dependencies between .NET assemblies.

IanT8
Thanks for your answer, but this is not what I was looking for. I'm writing the service by myself and want to find out, what dependencies I need to write there (or the installer needs to write).
Scoregraphic