Search code examples
wixcustom-action

Set environment variable before running a custom action in WiX


I have to build an MSI-based installer using WiX and I need to set environment MY_HOME before running a command action.

I have a component:

<Component Id="SEMYHOME"
           Guid="*my guid*">
    <CreateFolder />
    <Environment Id="MY_HOME"
                 Action="set"
                 Part="all"
                 Name="MY_HOME"
                 Permanent="no"
                 System="yes"
                 Value="[APPLICATIONPATH]myapp"/>
</Component>

Then I have a custom action:

<CustomAction Id="InstallMyService"
              Directory="INSTALLDIR"
              ExeCommand='&quot;[INSTALLDIR]myapp\install_service.bat&quot; install'
              Execute="immediate"
              Return="ignore"/>
<InstallExecuteSequence>
    <Custom Action="InstallMyService"
            After="InstallFinalize"/>
</InstallExecuteSequence>

NOTE: This action need the MY_HOME variable to be set before running.

When install this MSI, I got a log showing that the MY_HOME variable is set before running the custom action "InstallMyService", but the command to install my service still fails. I found that the cause is when command called, MY_HOME still not set.

After an install is finished, MY_HOME was set as expected, but the custom action fails :(

How can I fix this problem?


Solution

  • Windows Installer and Custom Actions are hosted via the Service Control Manager which has a long history of not respecting broadcast messages that are sent announcing Environment changes. So even if you fix the immeadiate / deferred problem that Yan mentions you'll find that your custom action still doesn't have the environment variable.

    Why don't just just pass "[APPLICATIONPATH]myapp" to your .bat file and fetch it in as %2?

    BTW I also don't reccomend calling batch files from an installer. It's fragile and embarrassing to see installs that run popping up little black windows.