Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 425
DCCD Avatar asked Jan 20 '23 21:01

DCCD


2 Answers

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.

like image 115
Christopher Painter Avatar answered Jan 23 '23 09:01

Christopher Painter


You CA is immediate. This means that it runs immediately when Windows Installer is processing your MSI package. And this obviously happens before the component containing <Environment/> is installed. Modify it to be deferred (Execute="deferred") and schedule before InstallFinalize.

like image 23
Yan Sklyarenko Avatar answered Jan 23 '23 10:01

Yan Sklyarenko