Passiing command line arguments to a service

M

MW

Hi all,

I want to pass arguments to my OnStart(args) method in a windows service
application.

I tested this out by
NET START myService /test

and it works fine. I however want to give my users the ability to use the
Start button from the Service Control Manager instead of typing NET START or
executing a batch file to the same affect.

To achieve this I thought about editing the registry where the location of
the service exe might be stored. I tried editing the ImagePath property of
the service but that did not work. OnStart did not recieve the arguments.
Incidently thats how Tomcat passes arguments to its service.

I cannot use configuration files or add my own registry settings for this
service and I am restricted to passing the argument. Any comments will be
most welcome.

Many thanks,
-Mujahid
 
M

mwazir

Hi John and Stephen,

When I added arguments to the SYSTEM\CurrentControlSet\Services\[My service
name]\ImagePath I found that its not the OnStart() event that recieves the
arguments but the Main function of the service class.

You can do two things
- Write another constructor that uses the arguments. Since an instance of
the service class is created in the main function, the value will be passed
or
- Expose a property that can be set after the service class has been
instatiated

I wanted to create multiple instances of my service each running in their
own process, pass the service name as an argument, based on which I could
look up the relevant registry and configuration files.
I posted my solution to another newsgroup. Here is the url to that thread.

http://www.dotnetsmart.com/DesktopDefault.aspx?tabid=23&frmView=ShowPost&PostID=5698

HTH
 
S

Stephen

Thanks,

For some reason never even thought about the main function, probably because
VS .net creates the OnStart method with the args parameter and provides
absolutely no documentation about when it will be set.

I now store the args supplied to the main function, then combine it with the
args supplied to the Onstart method and everything is working great.

Cheers!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top