Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a parameter to a windows service once and for all at install instead of each start

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.

like image 552
Francesca Avatar asked Sep 28 '09 19:09

Francesca


People also ask

How do I pass parameters to a Windows service?

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.

What is start parameters in service?

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.

How do I automatically install Windows services?

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.


2 Answers

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.

like image 162
Remus Rusanu Avatar answered Oct 20 '22 01:10

Remus Rusanu


How about putting the parameter in a config file?

like image 3
Raj More Avatar answered Oct 20 '22 00:10

Raj More