How to set an installer property using custom actions
Windows Installer properties are global variables that Windows Installer uses during an installation. There are two main types of Windows Installer properties:
- Private Properties (contain lower-case letters in their name, example: My_Prop)
- Public Properties (contain only upper-case letters in their name, example: MY_PROP)
The difference between Public and Private properties consists in the way their values are being passed on. Only the value of a Public Property is passed on from the Wizard Dialogs Stage (in which the dialogs are showed) to the Install Execution Stage (in which the system is modified).
In order to get or set an installer property, you can use the following methods:
A clear distinction must be made between custom actions and the methods used to access installer properties. Depending on the custom action's execution time, different approaches should be used to access the installer properties.
Only immediate custom actions have direct access to installer properties. The other custom actions, deferred, commit and rollback do not have access directly to installer properties. Instead, through an intermeddiary property called CustomActionData you can access the installer properties.
1. Get and set a property in a .NET Custom Action
1.1 Access properties in Immediate custom actions
// getting a property value string myProperty = "MY_PROPERTY"; string myPropertyValue = session.GetProperty(myProperty); // setting a property value session.SetProperty(myProperty, myPropertyValue);
1.2 Access properties in Deferred, Rollback or Commit custom actions through the CustomActionData property
Pass properties in the custom action throgu the Action data field that will set the CustomActionData property. When passing multiple properties, you need to add a separator character e.g. ; and for properties that might contain spaces, they need to be enclosed in quotes, as below:
The "|" character is reserved and should not be used as a
separator in the ActionData field. Feel free to choose an
alternative character for separating properties e.g. ";".
In the custom action, you can have something like this to access the ActionData property:
string customActionData = session.CustomActionData.ToString(); // Split the ActionData string using ';' as the delimiter string[] parameters = customActionData.Split(';'); // Assign each value to a local variable string appDir = parameters[0].Trim(); // Expected to be "[APPDIR]" string productName = parameters[1].Trim(); // Expected to be "[ProductName]" string productVersion = parameters[2].Trim(); // Expected to be [ProductVersion] // Optionally, remove the surrounding quotes if necessary appDir = appDir.Trim('"'); productName = productName.Trim('"'); productVersion = productVersion.Trim('"'); // Display an informative MessageBox for debugging purposes: System.Windows.Forms.MessageBox.Show(msiWindow, productName + " with version:" + productVersion + " will be installed in the following location: " + appDir, "Info");
You cannot set installer properties from a deferred, rollback or commit custom
action, only within an immediate custom action you can set an installer property.
To create a .NET custom action, first install the Advanced Installer extension in Visual Studio (VS). This will add specific project templates to VS you can use to create a C# Custom Action. After creating your dll, you can easily integrate it in your installer project. To do this, use the predefined Call method from .Net assembly custom action to call a function within your dll. For additional guidance, please refer to the How to create a .NET Custom Action article.
2. Get and set a property in a DTF C# custom action
2.1 Access properties in Immediate custom actions
// getting a property MyVariable = session["MyProperty"]; // setting a property session["MyProperty"] = "MyValue";
Check the Retrieving and setting properties in immediate custom actions article for details on how to access installer properties in immediate custom action.
2.2 Access properties in Deferred, Rollback or Commit custom actions through the CustomActionData property
Check the Pass installer properties in deferred custom action to see how to have access to installer properties in a deferred, rollback or commit custom action.
To call a function from a dll, you can use the predefined Call function from attached native DLL custom action. For details, please check the Options to integrate and debug custom actions article with related information for hints and guidance.
3. Get and set a property in Windows PowerShell Script custom action
3.1 Access properties in Immediate custom actions
# getting a property $propValue = AI_GetMsiProperty MY_MSI_PROP # setting a property AI_SetMsiProperty MY_MSI_PROP "MyValue"
3.2 Access properties in Deferred, Rollback or Commit custom actions through script parameters
This can be done through the Script Parameters' field, as follow:
-appdir "[APPDIR]" -productName "[ProductName]" -productVersion [ProductVersion]
APPDIR, ProductName and ProductVersion are MSI installer properties. Their values will be assigned to the $appdir, $productName and $productVersion parameters in the PowerShell script. You need to update the Param() block of the script, which must be on the first line, as follows:
Param($appdir, $productName, $productVersion)
4. Get and set a property in a C++ custom action
// getting a property MsiGetProperty(aHandle, L"MyProperty", MyVariable, &propertySize); // setting a property MsiSetProperty(aHandle, L"MyProperty", MyVariable);
For details, please check the MsiGetProperty function and MsiSetProperty function.
5. Get and set a property in a Visual Basic and Java scripts custom action
// getting a property MyVariable = Session.Property("MyProperty") // setting a property Session.Property("MyProperty") = "MyValue"
To execute a VBScript or a JavaScript you can use the predefined Execute inline script code or Launch attached file custom actions.
We recommend you to use a DLL custom action for example, you can create your custom action as a custom action written in C# or a custom action written in C++, instead of VBScript or JavaScript.