PROBLEM:

Recently, I needed to create multiple instances of the window service for the same executable. Each instance need to perform different task depending upon the parameter being passed at the startup. Hence, the question is how do i pass the parameter to the service instance?

SOLUTION:

 Within the Window Service project, Open the Program.cs class.  Modify the Main method signature to take array of string as argument if it does not already.

As shown above, Main method takes the parameter and pass it to the Service class to perform the specific task.

Once the service is installed, Start Parameters can be passed on via Properties window of the Service.  Goto Start > AdministrativeTools > Services window. Find the service in the list and right click on the service name. Click Properties from the menu to view Properties window. Under General tab at the bottom, you would see the text box for Start parameters. Type the parameter and click Start button.

Window Service Properties

Note: The start parameters passed via Properties textbox does not persist. Hence, for the service that need to start automatically, this won’t work.

Hence, we need to apply a different trick for services that need to persist startup parameter for auto start. For this, use the service registry key called ImagePath.

Goto Start > Run. Type Regedit and hit Enter.  In the Registry Editor window, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<ServiceName>. 

The ImagePath key contains the path to the executable as its value. Double click to edit the value. In the Value textbox, input the parameter after the path to executable with the space inbetween.

Value name: ImagePath

Value data: “Path to executable” “Parameter1” “Parameter2”

WindowService Registry ImagePath

That is it.

Happy Coding..

Passing parameters to window services
Tagged on:     

6 thoughts on “Passing parameters to window services

  • September 27, 2011 at 8:41 am
    Permalink

    Hi,
    Just that the persistent arguments (modified in registry) are different to the transient arguments (supplied every time) in that the persistent arguments are process scoped whereas the transient arguments are service scoped.
    In simple words, the persistent arguments are supplied to the Main() method and available for all services that run under the same process whereas the transient arguments are supplied to the OnStart() of the specific service.
    Hope it helps!!!

    Reply
  • November 29, 2011 at 1:19 pm
    Permalink

    Thanks for the information; it’s very helpful.

    Sadly I just discovered there’s a distinct difference between program start up parms and service start up parms.

    Passing parms at the command line as you do above will result in the “Program.Main()” method receiving the parameters.

    Passing parms at the services console results in the “Program.ServicesToRun.OnStart()” method receiving the parameters.

    What ruffles my feathers is the fact that the MSDN docs fail to explain the differences and now I have to go back and modify my application (windows service).

    Reply
  • April 6, 2012 at 12:50 am
    Permalink

    Hai…
    This article is very nice..
    currently i developed windows service in vb.net…
    How can i read the “MyService(taskName)” to the my class MyService..?
    i need to pass a connection string to my service as strat up parameter along with exe path..

    Please Help me..
    Any suggestion will acceptable.

    Reply
    • April 18, 2012 at 12:36 pm
      Permalink

      Can you tell me what you are trying to do?

      In my case, I am passing the taskname as an argument in the constructor of the MyService. You can define multiple parameters and pass arguments to a constructor.

      Reply
      • June 11, 2012 at 1:58 pm
        Permalink

        Yes. Argument is passed through constructor at the window service startup. Its mostly for automatic starting services so it retains the arguments.

        Reply
    • June 11, 2012 at 2:02 pm
      Permalink

      I didnt understand your question.

      Example of using parameter

      public class MyService
      {
      private string _taskName;

      public MyService(string taskName)
      {
      _taskName = taskName;
      }

      }

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *