views:

651

answers:

3

We have a Windows Service application that can accept command line parameters like:

MyService -option

So far, when we want to start the service with a parameter, we either do it manually from the Service Properties dialog (in the Start parameters box) or with the command

sc start MyService -option

What we would like is a way to install the service "permanently" with this parameter, so that the users would just have to start/stop it without having to set the parameter each time.

BTW, adding the parameter in the ImagePath registry entry doesn't work, neither does installing like this:

MyService -option /install

Updated: Thank you for the answers so far which help me refine the question.
What I'd like to achieve is to set the parameter at the Service level itself (like with the properties) in case there are more than 1 service in the same executable. The binpath config option is merely updating the ImagePath entry in the registry. That cannot be service specific.

+2  A: 
sc config MyService binPath= MyService.exe -option

Update

The individual service parameters are stored in the the registry at the key HKLM\SYSTEM\CurrentControlSet\Services\<serviceName>\Parameters. I'm not sure though how the parameters are passed to the service. I believe SCM reads these values then when it calls StartService it passes them to the ServiceMain callback.

Remus Rusanu
Equivalent to edit the ImagePath registry entry. Not specific to the service itself. But a good way to manage that entry anyway, thanks.
François
Good try, but it does not seem to work... (at least not like with properties or sc start)
François
StartService *does* pass as arguments to ServiceMain whatever is configured in the Service Properties 'Arguments' edit box from the services.msc snap in, that is documented in the spec. That should be good enough for you I believe.
Remus Rusanu
The spec for ServiceMain says that the parameters entered in that edit box are for when the user is starting the service ***manually***. They are not saved once the user has clicked OK, and they are not used when auto-starting services.
Aaron Klotz
A: 

How about putting the parameter in a config file?

Raj More
+1  A: 

Arguments passed on the command-line via ImagePath are accessible in main() or via GetCommandLine(). You could install with command-line args and then in your ServiceMain, check to see if any arguments were passed in the lpszArgs parameter. If not, call GetCommandLine and see if any were passed that way.

Dustin
Testing at both Exe and service level would be a solution for a single service Exe. But it would require to rewrite the service and would not work also with multiple services in the same Exe.
François
I think that Remus' answer about using the Parameters registry key is your only option here. I don't think that there is any way to get service parameters passed for auto start services.
Dustin