Windows Installer Basics: Conditions |
Conditions are "tests" that Windows Installer can perform to determine whether or not to do something, some sources of information that conditions can examine include:
See the Windows Installer documentation for more details on what can be tested and how, this page gives you the syntax of conditional statements (Just to be different conditions don't support "formatted strings", real smart).
Some examples of situations which use conditions are:
Its worth having a look at Heath Stewart's "Test your Conditions" blog entry.
WARNING: You need to be aware that some information sources have restrictions on when they can be sequenced, if a condition fails (whether always or sometimes) then you should double check the lifecycle of the items being examined (for example references to the "REMOVE" property can only be made after "InstallValidate" has run).
Predefined Conditions in MAKEMSI Macros |
MAKEMSI has predefined some handy macros which can be used to specify some common conditions:
#define? CONDITION_ALREADY_INSTALLED Installed ;;Repair, uninstall etc. #define? CONDITION_INSTALL_ONLY not Installed ;;Doesn't include a repair, uninstall etc! #define? CONDITION_UNINSTALL_ONLY Installed and REMOVE~="ALL" ;;Complete uninstall - HIGHLY RECOMMENDED at you read the "REMOVE" properties MAKEMSI doco! #define? CONDITION_EXCEPT_UNINSTALL not (<$CONDITION_UNINSTALL_ONLY>) ;;Install, Repair etc (all but complete uninstall) #define? CONDITION_IS_WIN2000 VersionNT = "500" ;;Is OS WIN2000? #define? CONDITION_IS_WINXP VersionNT = "501" ;;Is OS WINXP? #define? CONDITION_IS_VISTA VersionNT = "600" ;;Is OS WINDOWS Vista? #define? CONDITION_IS_WINDOWS_7 VersionNT = "601" ;;Is OS WINDOWS Windows7 #define? CONDITION_UI_NONE UILevel = 2 ;;Silent Install #define? CONDITION_UI_BASIC UILevel = 3 #define? CONDITION_UI_REDUCED UILevel = 4 #define? CONDITION_UI_FULL UILevel = 5 ;;"Normal" #define? CONDITION_UI_NO_DIALOGS UILevel <> 5 ;;Don't bother user with popup dialogs, opening readme files etc. #define CONDITION_PER_MACHINE Version9X or (ALLUSERS = 1) ;;True if per-machine install. #define CONDITION_PER_USER not (<$CONDITION_PER_MACHINE>) ;;True if per-user (not per-machine) install.
Use of Macros for Complex Conditions |
I recommend that you should create macros for complex conditions (like the above) and use the macros, this way the macros name is self descriptive making it easier to write, read and debug your code (it is obvious what is intended by the definition "CONDITION_UI_FULL").
If you used the macro "IF_REPAIRING_AND_FEATURE_X_IS_BEING_INSTALLED" in 6 places not only does it make it easier to read, but if you are diagnosing a problem and need to fix the condition you will only need to change one place. It is also easier to see if the condition doesn't match the name of the macro (let alone any comments you might have).
The following is a more extreme example of what I recommend, and these could be defined in your personal MAKEMSI configuration file:
;--- Define Feature and Component state queries ----------------------------- #define FeatureIs (!{$#1} <$INSTALLCOMPARE_{$IS=^EQUAL^}> <$INSTALLSTATE_{$STATE=^LOCAL^}>) ;;Feature is already installed #define FeatureWillBe (&{$#1} <$INSTALLCOMPARE_{$IS=^EQUAL^}> <$INSTALLSTATE_{$STATE=^LOCAL^}>) ;;Feature is being installed #define ComponentIs (?{$#1} <$INSTALLCOMPARE_{$IS=^EQUAL^}> <$INSTALLSTATE_{$STATE=^LOCAL^}>) ;;Component is already installed #define ComponentWillBe (${$#1} <$INSTALLCOMPARE_{$IS=^EQUAL^}> <$INSTALLSTATE_{$STATE=^LOCAL^}>) ;;Component is being installed ;--- Define valid "IS" parameter comparison types --------------------------- #define INSTALLCOMPARE_EQUAL = #define INSTALLCOMPARE_NOT_EQUAL <> ;--- Define valid "STATE" parameter values ---------------------------------- #define INSTALLSTATE_UNKNOWN -1 ;;No action to be taken on the feature or component. #define INSTALLSTATE_ADVERTISED 1 ;;Advertised feature. This state is not available for components. #define INSTALLSTATE_ABSENT 2 ;;Feature or component is not present. #define INSTALLSTATE_LOCAL 3 ;;Feature or component on the local computer. #define INSTALLSTATE_SOURCE 4 ;;Feature or component run from the source.
Some examples of comparisons using the above definitions (these must be sequenced after "CostFinalize"):
... Condition=^<$FeatureIs "COMPLETE" STATE="LOCAL">^ ;;Feature "COMPLETE" is CURRENTLY installed locally ... Condition=^<$FeatureWillBe "COMPLETE" STATE="LOCAL">^ ;;Feature "COMPLETE" is BEING installed locally (its not now) ... Condition=^<$FeatureIs "COMPLETE" IS="NOT_EQUAL">^ ;;Feature "COMPLETE" is CURRENTLY NOT installed locally
Some Example Conditions |
The Windows Installer documentation also has some examples, others can be seen throughout this manual, including at:
Other examples of conditions are: