When it comes to creating a robust and efficient installation process for your software, WiX is a powerful tool that can greatly simplify the process. However, there may be times when the default features of WiX may not be enough to meet your specific needs. In such cases, the ability to add custom actions can be a game-changer. In this article, we will discuss how to add a WiX custom action for uninstall only, via MSI.
Before we dive into the technicalities, let's first understand the need for having a custom action for uninstall only. In many cases, when you want to uninstall a software, you may not want to remove all the files and folders associated with it. You may want to keep certain files, such as user preferences or application data, intact. This is where a custom action for uninstall only can come in handy.
To begin with, we need to create a new WiX project or open an existing one. Next, we need to add a new "CustomAction" element inside the "Product" element, like this:
<CustomAction Id="CustomActionUninstall" Return="check" Execute="immediate" FileKey="UninstallActionDll" ExeCommand="CustomUninstallAction">
In the above code, we have specified the "Id" of the custom action as "CustomActionUninstall" and set the "Execute" attribute to "immediate". This means that the custom action will be executed during the immediate phase of the installation process.
Next, we need to define the "FileKey" and "ExeCommand" attributes. The "FileKey" attribute specifies the name of the file that contains the custom action code, while the "ExeCommand" attribute specifies the name of the function that will be executed when the custom action is triggered. In our case, we will create a DLL file named "UninstallActionDll" and define a function named "CustomUninstallAction" inside it. This function will contain the code for our custom uninstall action.
Now, we need to define the "InstallExecuteSequence" element, which is responsible for executing the custom action during the uninstall process. We will add a new "Custom" element inside it, like this:
<CustomAction Id="CustomActionUninstall" Return="check" Execute="immediate" FileKey="UninstallActionDll" ExeCommand="CustomUninstallAction" />
The above code will ensure that our custom action will be executed during the uninstall process. However, we also need to specify the "Remove" attribute, which will tell WiX to only execute the custom action when the software is being uninstalled, not during installation or repair.
<CustomAction Id="CustomActionUninstall" Return="check" Execute="immediate" FileKey="UninstallActionDll" ExeCommand="CustomUninstallAction" Remove="all" />
With this, our custom action for uninstall only is ready to be used. However, we also need to add the necessary components and files to our WiX project. We need to add the DLL file containing the custom action code and also add the necessary registry keys and values for the custom action to work properly. This can be done by creating a new "Component" element and adding the appropriate "RegistryKey" and "RegistryValue" elements inside it.
Once all the necessary components and files are added, we can build and test our installation package. During the uninstall process,