RajSanpui
RajSanpui

Reputation: 12074

Extract environment variable at install time in msi (wxs)

My idea is to extract the value of environment variable ZEN_HOME and assign it to variable MyInstallDir at install time, and if it is not set, then set the variable with some other value ProgramFileFolder.

The error i am facing is, wxs at compile time, is searching for the value, instead of install time.

How to make sure that the value is extracted at install time and not compile time?

<?if %ZEN_HOME% != "" ?>
    <?define MyInstallDir = %ZEN_HOME% ?>
<?else?>
    <?define MyInstallDir="ProgramFilesFolder" ?>
<?endif?>

Upvotes: 1

Views: 2163

Answers (1)

Yan Sklyarenko
Yan Sklyarenko

Reputation: 32270

What you are trying to do will execute at compile time, as you correctly mentioned, during preprocessing. You can't leverage WiX variables at install time - it's totally WiX custom concept Windows Installer knows nothing about.

So, if I understand your intention correctly, you're going to set the installation directory of your application to some value of environment variable, if it's there on a target machine. Otherwise, fall back to a folder under Program Files.

You can approach it in the following way. First, define the directory structure similar to this:

<Directory Id="TARGETDIR" Name="SourceDir">
   <Directory Id="ProgramFilesFolder">
      <Directory Id="INSTALLLOCATION" Name="MySetupProject">
         ...
      </Directory>
    </Directory>
</Directory>

This will serve as a fallback. Later, define a set-a-property custom action to set INSTALLLOCATION in case there's an environment variable defined:

<SetProperty Id="INSTALLLOCATION" Value="[%ZEN_HOME]" After="CostFinalize">[%ZEN_HOME]</SetProperty>

You should schedule it after CostFinalize to be able to address directories as properties.

Upvotes: 5

Related Questions