paul
paul

Reputation: 1705

How can I define this dependency when installing a service

I'm dealing with a circular dependency w.r.t. a Windows service and its dependence upon a driver. The situation is as follows:

I wrote a Windows service in C++. This service depends on a DLL which installs a driver the first time it is loaded/used; the DLL uses the driver during operation thenceforth.

In early testing I would install the service via a command-line call to the service executable with the /INSTALL flag -- this would call InstallService internally.

Then I ran into an issue: the service would run when I started it manually, but when it was set to run automatically after boot it would start before the driver had been loaded and it would error because of this.

To fix this, I set up a dependency for the service upon the driver -- Windows treats drivers similar to services -- in the service's registry entry @ HKLM\System\CurrentControlSet\Services\<SERVICE> in the DependOnService value. This fixed that issue.

Fast-forward to deployment: I am using WiX to install and start the service. During installation, I want to set the driver up as a dependency of my service. However, if I define the driver as a dependency in the ServiceInstall element then WiX tries to start it before starting my service, thus my problem: WiX can't start the service without starting the dependency, and the dependency isn't there because the service hasn't run yet.

If I don't specify the dependency in the installer configuration file then the service installs and runs fine.

I figured that I could define the dependency after installing the service, in the registry like before, but there's no entry for the service there!

Another thing: the service runs after boot now! Without having defined the dependency! I haven't done much testing to see if this is consistent or not.

So, how does WiX tell Windows that there's a service, i.e. why is there no registry entry? How can I specify the dependency? Do I even need to specify one anymore? Should I forego using the WiX service-related elements and run the commands I did during testing manually in the installer?

I've been wrestling with this and researching for at least a week; any insight would be appreciated.


Environment Info:

Upvotes: 0

Views: 1763

Answers (1)

paul
paul

Reputation: 1705

I found a solution.

Much of the issue was an RTFM problem (though to my defense the documentation wasn't all that clear):

I had been using a generic name for the value of the ServiceInstall element's Name attribute -- I was under the impression that this was used for linking the ServiceInstall element to the ServiceControl element.

The documentation for the ServiceInstall element says the following about the Name attribute:

This column is the string that gives the service name to install.

The documentation for the ServiceControl element says:

Name of the service.

The actual use for the value of the Name attribute is the hidden, system name of the service; this is the name used in the registry under HKLM\System\CurrentControlSet\Services. My service was thus being installed under the ServiceManagement key, since that's what I had in the Name attribute.

The different name is what was causing things to work dependency-wise. Apparently, barring dependencies, Windows loads services in order alphabetically (see the comments on this answer). When I was installing manually, my service had a name that came before that of the driver, so it was erroring when the dependency was not specified. The generic name I had specified in the Name attribute of the WiX project came after the driver's service name, so the driver was getting loaded before my service was.

What I ended up doing was switching the Name back to the proper name of the service then adding a RegistryValue to the WiX project to specify my service's dependency on the driver (service).

Upvotes: 1

Related Questions