Retrieving properties in deferred installation mode

MSI Installations run in two halves.  The first half is the “Immediate Execution Mode” where object references are calculated to construct a temporary précis of where the files and registry keys will be installed, along with evaluating variables and statements used within the MSI.  Once the précis has been created, the installer effectively has a specific install script for the target machine.  The second stage or “Deferred Execution Mode” is when the installation script is being acted upon.  During this stage, the earlier MSI properties and variables are no necessary or accessible as the installation script has already been created.

CustomActionData

As an example, Autofax is an application that utilises text based INI files.  Within its INI file in needs a location in short naming convention to the location of WINWORD.EXE.  The location of Winword.exe (if it exists) can easily be determined by a System Search that sets a property (in this case [WINWORD])

When a file called Winword.exe is found, the example above will set the property “WINWORD” to the full fine name of the first WinWord it has located in the specified Search area.

This particular scenario will need to have access to two properties to be able to adequately add the short name & path format of the Winword executable to the Autofax INI file.  The first property (Location of Winword.exe) we already know, the second needed information is where the INI file is going to be as we can’t guarantee that everyone will choose the default install location for the application.  This second needed property will be in INSTALLDIR property.  The particular file that needs to be appended to won’t be on the machine until after the deferred stage of the MSI install has started so we need a method of making the properties INSTALLDIR and WINWORD available to our script in the deferred install section.

This may be achieved by the special property “CustomActionData”.

CustomActionData

Although the second stage of an MSI install can no longer access the properties from the first stage, a Custom Action in deferred execution may access a single string called CustomActionData if a property with the same name as the Custom Action has been set during the first or “Immediate Execution” stage.  The importance is that the property set has exactly the same name as the Custom Action which will run.

This scenario has a Custom Action (CustomAction1) which will be called in the Immediate Execution secuence which will set the value of the property Rhubarb to the values of our WINWORD and INSTALLDIR properties.  We have decided to use a semicolon as a separator between the two values.

We will then utilise VBScript with a Custom Action called Rhubarb that will run in the Deferred execute sequence before “InstallFinalise”.  With VBScript will with then “unpack” the values that we have placed in the CustomActionData Property.

 The string passed as part of the CustomActionData property can be accessed by the VBScript command:  Session.Property("CustomActionData").  In this particular example, we will retrieve the CustomActionData string and unpack the variables by using an array.  The script would be:

Option Explicit

Dim PropArray, WINWORD, INSTALLDIR

Dim PropArray

PropArray = Split(Session.Property("CustomActionData"), ";")

WINWORD = PropArray(0)

INSTALLDIR = PropArray(1)

The properties for WINWORD and INSTALLDIR may be used within the VBScript elsewhere.

 

The full script used within this custom action is as follows:

 

Option Explicit

‘Declare Variables

Dim objFSO, objFolder, objShell, objTextFile, objFile, f, s

Dim PropArray, strFile, strText, WINWORD, Shortname, INSTALLDIR

‘Create FileSystem Object

Set objFSO = CreateObject("Scripting.FileSystemObject")

‘Unpack Values from CustomActionData string

PropArray = Split(Session.Property("CustomActionData"), ";")

WINWORD = PropArray(0)

INSTALLDIR = PropArray(1)

Set f = objFSO.GetFile(WINWORD)

Shortname=f.ShortPath

‘Open our Text file for writing

' OpenTextFile Method needs a Const value

' ForAppending = 8 ForReading = 1, ForWriting = 2

Const ForAppending = 8

strFile = INSTALLDIR &"AutoFax.ini"

Set objTextFile = objFSO.OpenTextFile (strFile, ForAppending, True)

' Writes strText every time you run this VBScript

objTextFile.WriteLine("[OFFICE]")

objTextFile.WriteLine("WINWORDPATH="&ShortName)

objTextFile.Close