4

How to pass arguments to installutil MyService.exe Parameter1 Parameter2

I MyService is a windows service. I want to pass the arguments to this service.

user2075283
  • 41
  • 1
  • 3

3 Answers3

4

override the install method of ProjectInstaller class

public override void Install(System.Collections.IDictionary stateSaver)
        {
              base.Install(stateSaver);
              string lParam1 = Convert.ToString(GetParam("Param1"));  

        }

        private object GetParam(string p)
        {
            try
            {
                if (this.Context != null)
                {
                    if (this.Context.Parameters != null)
                    {
                        string lParamValue = this.Context.Parameters[p];
                        if (lParamValue != null)
                            return lParamValue;
                    }
                }
            }
            catch (Exception ex )
            {

            }
            return string.Empty;
        }

and install the service using installutil /Param1=value path_to_your_exename

Sumeshk
  • 1,980
  • 20
  • 33
1

First you need to insert an installer to your service. But I guess you're already over that. Otherwise please read this:

https://learn.microsoft.com/en-us/dotnet/framework/windows-services/how-to-add-installers-to-your-service-application

As @Sumeshk mentioned you can pass arguments to this installer with the installutil like installutil /Param1=value path_to_your_exename and retrieve these informations in your installer code by this.Context.Parameters.

Now you want to pass these parameters as arguments to your service. The trick is to attach them to the assemblypath before the installation as you can see here:

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

Therefor you have to override the installer method OnBeforeInstall. Then you can manipulate the given assemblypath by attaching your parameters. Don't forget to call the base method OnBeforeInstall at the end:

protected override void OnBeforeInstall(IDictionary savedState)
{
    string oldPath = this.Context.Parameters["assemblypath"];
    string yourParam = this.Context.Parameters["Param1"];
    string pathWithParam = $"\"{oldPath}\" \"{yourParam }\"";
    this.Context.Parameters["assemblypath"] = pathWithParam;
    base.OnBeforeInstall(savedState);
}

Please note, to avoid problems with possible spaces in your parameter or the assembly path, you should frame these strings in double quotes. Seperate every parameter with a whitespace. If doing so, the ImagePath of your service in the registry will be composed of the path plus separated by a space your parameter. You can find this in the key HKLM\SYSTEM\CurrentControlSet\Services\<your service name>

Now you can evaluate your parameter in your service in the args parameter of the method OnStart:

protected override void OnStart(string[] args)
{
    Debug.WriteLine("Param1: {args[0]}");
}
Rouven
  • 324
  • 3
  • 12
0

I have too little reputation to comment on @Rouvens post. I foremost have to thank him for his answer. However, I would like to make a small correction.

The arguments you register for HKLM\SYSTEM\CurrentControlSet\Services\<your service name> through the installer component are NOT directly passed to OnStart(string[] args) as he claims.

Instead they follow in the constructor of the service:

public partial class MyService : ServiceBase
{
    string myArgument;

    public MyService(string[] args)
    {            
        InitializeComponent();
        
        this.myArgument= {args[0]}; // this is where you capture your argument
    }
}

Afterwards your have to pass these values without consideration for OnStart's arguments.

protected override void OnStart(string[] args)
{
    BusinessLogicControl.Main(new string[] { myArgument});
}
elena.kim
  • 930
  • 4
  • 12
  • 22