The following article uses options that are available starting with the Professional edition and project type.
Windows Installer does not offer support for detecting or stopping a service on the target machine. However, in Advanced Installer this can be done through a predefined custom action.
You can detect if a service is started, stopped or exists by using a "Detect service" predefined custom action. This custom action can be added at any point of the installation. If you want it to run at the beginning of the install process you can schedule it before "Wizard Dialogs Stage" -> "Searches". It should also be dragged (holding SHIFT key) before "Install Execution Stage" -> "Searches" to make sure that it runs even if there is no UI.
Since it uses installer properties, this custom action can only run as Immediate.
After adding the custom action, in its Custom Action Properties page, the Process Name field should be set to the name of the service you want to detect. When the custom action runs, it will set the AI_SERVICE_STATE property to one of these values:
- Started - the service is installed and it is started
- Stopped - the service is installed, but it is stopped
- Not Found - the service is not installed
The AI_SERVICE_STATE property can be used as a Custom Launch Condition in order to stop the installation based on the service state.
In order to stop a running service, you can use a Stop Service predefined custom action. If you want the custom action to run in the beginning of the installation, you can schedule it before "Wizard Dialogs Stage" -> "Searches".
It should also be dragged before "Install Execution Stage" -> "Searches" while the SHIFT key is pressed to make sure it runs even if there is no UI. In this case the custom action should use the "Execute only once if present in both sequence tables" option in the Custom Action Properties page.
After adding the custom action, in its "Custom Action Properties" page, the Service name field should be set to the name of the service you want to stop.
Stopping a service may require Administrator privileges. In this case, the custom action should be configured to Run under the LocalSystem account with full privileges(no impersonation), When the system is being modified (deferred). Since deferred custom actions can only run after "Preparing" action group, your custom action should be scheduled accordingly.