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.
You can pass parameters on startup like this: Right click on MyComputer and select Manage -> Services and Applications -> Services. Right click on your service, select Properties and you should then see the Start Parameters box under the General tab.
Startup parameters change the behavior of the Service Manager server. You can always set a startup parameter from the server's operating system command prompt. Parameter.
In your Installer class, add a handler for the AfterInstall event. You can then call the ServiceController in the event handler to start the service. Now when you run InstallUtil on your installer, it will install and then start up the service automatically.
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.
How about putting the parameter in a config file?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With