It is often desirable for an installer to be provided with specific data at the time of the installation. For example to supply a user name, password, license string etc. Passing command line arguments while initiating the installation process allows us to do this. This article gives you a detailed description about how to pass data to custom actions in a WIX Installer using command line arguments.
The installation process can be controlled using custom actions. Custom actions can be dynamic linked libraries, Visual Basic scripts or JavaScript files. A custom action written in C# will generate a dynamic linked library file (DLL).
Why use Custom Actions?
The installer provides many standard actions that execute during an installation. However, there may be times where your installation needs expanded functionality. In these cases, custom actions let you extend the capabilities of standard actions. We can use custom actions for the following scenarios.
- Launch an executable during installation that is installed on the user’s machine or that is being installed with the application.
- Call special functions during an installation that are defined in a – (DLL).
- Use functions written in the development languages Microsoft Visual Basic Scripting Edition or Microsoft JScript literal script text during an installation.
- Defer the execution of some actions until the time when the installation script is being executed.
- Add time and progress information to a ProgressBar control and a TimeRemaining Text control.
Creating a sample project.
In this sample project, we tell the Wix Installer to use custom actions to execute an .msi with command line arguments. The example will create a sample installer for a Windows Form Application.
Start Visual Studio and go to “File->New Project” under the installed section select Windows Forms Application [Figure1].
Figure 1
Create an Installer for the application
Right click on the solution and “Add->New Project“. Under Wix Toolset select Setup Project and add a new one. To install Wix Toolset, go to http://wixtoolset.org/releases/ download, setup and install [Figure2].
Figure 2
In the CustomActionSetup project, right-click on the References node and choose Add Reference.
Navigate to the Projects tab, click on the SampleApp project, and click the Add button, and then press OK [Figure3].
Figure 3
Create a Custom Action for the application.
Once we’ve created our setup project, we’ll need to add a C# Custom Action Project to our solution (you can see this project template option in the below screenshot as well).
To create a custom action, right click on the solution and “Add->New Project“. Under Wix Toolset select a “C# Custom Action Project” and add a new one [Figure 4].
Figure 4
Here is the final Solution [Figure 5].
Figure 5
Edit Product.wxs in Wix Setup Project
After creating the setup project you’ll have a Product.wxs file that defines your setup. In the Product.wxs file, we can define custom actions and properties which are sent through the command line.
- Define two properties “LIC” and “NAME”. See more on the Property Element in Wix here:
http://wixtoolset.org/documentation/manual/v3/xsd/wix/property.html
- Binary tag a link to the DLL. Here you will need to give a source location for the dll with any unique ID value. When we build the installer, a custom action project (.CA.dll) is created in the bin folder.
- In the CustomAction tag, you need to define which custom action you are using. In here refer to the “GetArguementValues” custom action and add the Binary Id to the BinaryKey property.
- After that define the InstallExecuteSequence for a given custom action. Here specify in which step of the installation the custom action should be executed. See more on the InstallExecuteSequence here:
http://wixtoolset.org/documentation/manual/v3/xsd/wix/installexecutesequence.html
The screenshot below shows the location of these elements in the Product.wix file [Figure 6]
Figure 6
Custom Actions
Within the custom action project, you’ll have a new CustomActions class. In the CustomActions class define a method “GetArguementValues” which we have previously added to the “DllEntry” property in the product.wxs file. Here we get a “LIC” and “NAME” property through a Session object. A custom action can interact with an installing package through “Session objects”. The Session object controls the installation process by opening the installer, which contains the installation data. Finally show in a message box with two parameters [Figure 7].
Pass Command Line Arguments to Wix Installer
You’ve most likely run .msi files simply by double-clicking them from Windows Explorer. However,you can also run an .msi file by typing the file name at the command line. The advantage of running from the command line is that you can pass parameters to the command.
Open a command prompt and ‘Run as administrator’. Figure [8]. To do this click the start button on Windows and search cmd, right click on command prompt and ‘Run as Administrator.
Figure 8
Using command line, we can run the .msi installer and pass parameter values which we define d in the Product.wxs. First go to the location where the .msi file is saved. For this example it’s located in “C:\pro”. Then, using the command msiexec /i CustomActionSetUp.msi LIC=”xxxxxxxxxx” NAME=”davton”, as shown below, you can run the installer and pass the parameter values [Figure 9].
Figure 9
When executing the command, it will open the install wizard and show the message box with the two property values being passed in the command line [Figure 10].
Figure 10
Summary
This article provides information for creating a custom action in Wix, passing parameters to custom action functions and extracting parameters passed from an installer inside a custom action function.
Written by Tharaka Pathirana May 2017