wixsharp icon indicating copy to clipboard operation
wixsharp copied to clipboard

Prevent Windows Service from being removed automatically during WIX MSI uninstall

Open MithileshL-26 opened this issue 8 months ago • 1 comments

I’m using an MSI installer (built with the WiX ServiceInstall element) and a Windows Service is created. The service installs correctly, and I start it manually in the Services.

When uninstalling the MSI through “Add or Remove Programs,” I encounter two scenarios:

**1: Service not running: ** If the service is not running, the MSI uninstalls cleanly, and service is removed (this works as expected).

**2: Service running: ** If the service is running, the MSI still uninstalls, and service is removed automatically.

I want when the service is running it should show below default windows message box while uninstalling the msi.

Image

Code for the Service Installation:

new ServiceInstaller
{
    Name = "IP.ServiceInstaller.exe",
    DisplayName = "[IASSERVICENAME]",
    Description = "This is a Service",
    StartOn = null, //set it to null if you don't want service to start as during deployment
    StopOn = SvcEvent.InstallUninstall,
    RemoveOn = SvcEvent.Uninstall_Wait,
    DelayedAutoStart = false,
    ServiceSid = ServiceSid.none,
    FirstFailureActionType = FailureActionType.restart,
    SecondFailureActionType = FailureActionType.restart,
    ThirdFailureActionType = FailureActionType.runCommand,
    RestartServiceDelayInSeconds = 30,
    ResetPeriodInDays = 1,
    PreShutdownDelay = 1000 * 60 * 3,
    Account = "[USERNAME]",
    Password = "[PASSWORD]"
}),

What are the changes should be for the given code? Can we change the StopOn or RemoveOn properties to prevent the service from being removed automatically and instead show the above Windows “service in use” message?

MithileshL-26 avatar Apr 24 '25 08:04 MithileshL-26

The installation of services is controlled by WiX built-in elements ServiceInstaller and ServiceControl. See https://docs.firegiant.com/wix/schema/wxs/servicecontrol/#_top

I do not see any attributes there that can possibly control how to pop up that dialog. Though there can be something in the WiX documentation.

ChatGPT could not help but suggest to do it by yourself from the custom action/event handler:

    var processName = "MyApp"; // no ".exe"
    var isRunning = Process.GetProcessesByName(processName).Any();

    if (isRunning)
    {
        session.Message(InstallMessage.Error, new Record { FormatString = "Please close 'MyApp' before continuing installation." });
        return ActionResult.Failure;
    }

    return ActionResult.Success;
}

oleg-shilo avatar Apr 24 '25 11:04 oleg-shilo

Hello @oleg-shilo,

By setting StopOn = null and RemoveOn = null, the default Windows message box popped up. Will this cause the service to work differently than usual?"

Even writing the custom action solution worked as well.

MithileshL-26 avatar May 08 '25 09:05 MithileshL-26

Will this cause the service to work differently than usual?

I cannot answer this question. StopOn and RemoveOn are the attributes that are described by the WiX schema, and unfortunately,y it does not explain what will happen if the user does not set these attributes:

https://docs.firegiant.com/wix3/xsd/wix/servicecontrol/

oleg-shilo avatar May 08 '25 12:05 oleg-shilo