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.
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 callsStartService
it passes them to theServiceMain
callback.Use the SC (service control) command, it gives you a lot more options than just start & stop.
If there is more than one service with the same executable then you would be installing them with different service names. You could refer to the service name instead of the parameters.
To get the service name you can use this How can a Windows Service determine its ServiceName?
How about putting the parameter in a config file?
According to the ServiceBase.OnStart documentation:
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.