Friday, June 18, 2010

Accessing or Setting Windows Installer Properties Through Deferred, Commit, and Rollback Custom Actions

Project Type:
Basic MSI, InstallScript MSI



Hi Friends, often we need to use deferred type custom actions in your project. Many readers will wonder what the hell are these deffered custom actions. Don't worry my friends, I am here at your help.

For help about Deffered custom actions, please read my post named "Deffered Custom Actions".

In Deferred, Commit, and Rollback type custom actions, we are having access to only some of the in-built Windows Installer properties like:
CustomActionData, ProductCode, and UserSID.

If we want to access any other properties during the Deferred, Commit, and Rollback execution, then we need to pass those properties in CustomActionData.
 We need to schedule an immediate set-a-property type of custom action to set a property that matches the name of the custom action. The value of this property is then available in the CustomActionData property within the deferred, commit, or rollback custom action.

Using CustomActionData to Access a Property:
Following example will show you how to access the Windows Installer property SUPPORTDIR through a deferred InstallScript custom action.
  1. In the Custom Actions and Sequences view, create a set-a-property custom action (type 51) called GetSUPPORTDIR. Configure the Property Name, Property Value, and Install Exec Sequence settings for the custom action as follows, and leave all of the other settings blank.
    • Property Name: DisplaySupportDir
    • Property Value: [SUPPORTDIR]
    • Install Exec Sequence: After InstallInitialize
  2. In the InstallScript view, create a new function called DisplaySupportDir.
  3. Add the following code to display a message box containing the value of SUPPORTDIR:

    function DisplaySupportDir(hMSI)
       STRING supportDirPath;
       NUMBER supportDirPathBuffer;
    begin
       supportDirPathBuffer = MAX_PATH;
       if(MsiGetProperty(hMSI, "CustomActionData", supportDirPath, supportDirPathBuffer) == ERROR_SUCCESS) then
         SprintfBox(INFORMATION,"Deferred Execution","The value of SUPPORTDIR is %s",supportDirPath);
         SprintfBox(INFORMATION,"Deferred Execution","The value of InstallScript's SUPPORTDIR is %s",SUPPORTDIR);
       endif;
    end;
  4. In the Custom Actions and Sequences view, create an InstallScript custom action called DisplaySupportDir. Configure the Function Name, In-Script Execution, and Install Exec Sequence settings for the custom action as follows, and leave all of the other settings blank.
    • Function Name: DisplaySupportDir
    • In-Script Execution: Deferred Execution in System Context
    • Install Exec Sequence: After GetSUPPORTDIR

You could also use the below Installscript custom action which I had used in most of my projects for accessing multiple properties set in CustomActionData.

For example, if you want to retrieve the values of [INSTALLDIR], [SUPPORTDIR], and [SetupType], you would set the Property Value setting of your type 51 custom action to this:

[INSTALLDIR];[SUPPORTDIR];[SetupType]

where each property is separated by a semicolon.
----------------------------------------------------------------------------------------------
STRING sPropArray(2);

MsiGetProperty(hMSI, "CustomActionData", svProp, nLength);


listID = ListCreate (STRINGLIST);
if (StrGetTokens (listID, svProp, ";") > 0) then
//MessageBox ("StrGetTokens failed.", SEVERE);
else
nIndex=0;
// Get the first number from the list.
nResult = ListGetFirstString(listID, svString);
while (nResult != END_OF_LIST)
NumToStr(sIndex,nIndex);
//MessageBox (sIndex, SEVERE);
sPropArray(nIndex)=svString;
nIndex=nIndex+1;
nResult = ListGetNextString (listID, svString);
endwhile;
endif;
ListDestroy (listID);
---------------------------------------------------------------------------------------------
http://kb.flexerasoftware.com/selfservice/viewContent.do?externalID=Q104413

No comments:

Post a Comment