108

I have a .Net Windows service. I want to create an installer to install that windows service.

Basically, it has to do the following:

  1. Pack installutil.exe (Is it required?)
  2. Run installutil.exe MyService.exe
  3. Start MyService

Also, I want to provide an uninstaller which runs the following command:

installutil.exe /u MyService.exe

How to do these using Inno Setup?

1
  • I think you need to use the [Run] section. See here Sep 20, 2009 at 1:16

5 Answers 5

243

You don't need installutil.exe and probably you don't even have rights to redistribute it.

Here is the way I'm doing it in my application:

using System;
using System.Collections.Generic;
using System.Configuration.Install; 
using System.IO;
using System.Linq;
using System.Reflection; 
using System.ServiceProcess;
using System.Text;

static void Main(string[] args)
{
    if (System.Environment.UserInteractive)
    {
        string parameter = string.Concat(args);
        switch (parameter)
        {
            case "--install":
                ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                break;
            case "--uninstall":
                ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                break;
        }
    }
    else
    {
        ServiceBase.Run(new WindowsService());
    }
}

Basically you can have your service to install/uninstall on its own by using ManagedInstallerClass as shown in my example.

Then it's just matter of adding into your InnoSetup script something like this:

[Run]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--install"

[UninstallRun]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--uninstall"
9
  • 3
    you can try Filename: "net.exe"; Parameters: "start WinServ". if it doesn't work, you could just add one more switch --start to your c# application and start windows service directly from the program by using ServiceController class (msdn.microsoft.com/en-us/library/…). Sep 20, 2009 at 2:52
  • 2
    +1 Nice. See also stackoverflow.com/questions/255056/… Jan 29, 2010 at 9:24
  • 7
    For the C# neophyte (like me), you either need to add a using System.Reflection; or change Assembly to System.Reflection.Assembly in the code above.
    – rlandster
    Oct 9, 2011 at 19:16
  • 1
    InstallUtil is a part of dot net framework, you don't need "rights" to redistribute it, it's present on your target system already (assuming you can run your app in the first place of course) May 23, 2014 at 6:22
  • 10
    From the documentation about the InstallHelper method in 4.5 - "This API supports the .NET Framework infrastructure and is not intended to be used directly from your code." Discovered after receiving System.InvalidOperationException.
    – Bratch
    Jun 9, 2014 at 17:01
8

Here's how i did it:

Exec(ExpandConstant('{dotnet40}\InstallUtil.exe'), ServiceLocation, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

Apparently, Inno setup has the following constants for referencing the .NET folder on your system:

  • {dotnet11}
  • {dotnet20}
  • {dotnet2032}
  • {dotnet2064}
  • {dotnet40}
  • {dotnet4032}
  • {dotnet4064}

More information available here.

7

You can use

Exec(
    ExpandConstant('{sys}\sc.exe'),
    ExpandConstant('create "MyService" binPath= {app}\MyService.exe start= auto DisplayName= "My Service" obj= LocalSystem'), 
    '', 
    SW_HIDE, 
    ewWaitUntilTerminated, 
    ResultCode
    )

to create a service. See "sc.exe" on how to start, stop, check service status, delete service, etc.

2

If you want to avoid reboots when the user upgrades then you need to stop the service before copying the exe and start again after.

There are some script functions to do this at Service - Functions to Start, Stop, Install, Remove a Service

1
  • In your linked article prototypes of the used functions are not precisely translated and their usage is also not correct (there is e.g. no waiting for the service to start, stop etc.).
    – TLama
    Dec 11, 2014 at 5:27
0

have a look at topshelf http://topshelf-project.com/

  • it lets you develop your service as a console application

  • adds a start/stop service as an API to your service...

  • ... that you can call from InnoSetup

    [Run] Filename: "{app}\myservice.exe"; Parameters: "stop" ; Flags : waituntilterminated Filename: "{app}\myservice.exe"; Parameters: "uninstall" ; Flags : waituntilterminated Filename: "{app}\myservice.exe"; Parameters: "install -description ""myservice""" ; Flags : waituntilterminated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.