Set environment variable before running a custom action in WiX

眉间皱痕 提交于 2019-12-07 11:58:36

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4515905/set-environment-variable-before-running-a-custom-action-in-wix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!