Wix4 How to start a GUI elevated application after install

110 Views Asked by At

I need to start a GUI application as Elevated user (Administrator) after install finalizes, and I want also to check the exit code of the application.

I know on a CustomAction you can set Impersonate to false to execute it as SYSTEM user. But my application cannot run as SYSTEM user, it needs to be executed as Administrator or elevated user.

If instead I set Impersonate to true, the app is started as regular user and UAC is not shown, so my application has not enough rights to be executed.

The only trick that I found is to launch my application through, cmd, but in this way I lose the exit code, and setup completes successfully before my application is started.

<CustomAction Id="TRIGGER_CONFIGURE_AFTER_INSTALL_FINALIZE" Execute="immediate" Return="check" ExeCommand="cmd.exe /c start /b configure.exe &amp;&amp; exit 0" Directory="INSTALLDIR"></CustomAction>
<InstallExecuteSequence>
    <Custom Action="TRIGGER_CONFIGURE_AFTER_INSTALL_FINALIZE" After="InstallFinalize" Condition="NOT Installed"></Custom>
</InstallExecuteSequence>
1

There are 1 best solutions below

0
On

To start a GUI application as an elevated user after the installation finalizes in a WiX setup, you can use the CustomAction element with ExeCommand to run a command that launches the application with elevated privileges. However, you need to be careful about handling the exit code.

Example of how you can achieve this:

<CustomAction Id="TRIGGER_CONFIGURE_AFTER_INSTALL_FINALIZE" Execute="immediate" Return="asyncNoWait" Impersonate="no"
              ExeCommand='cmd.exe /C "start /wait "" "[INSTALLDIR]configure.exe"" &amp;&amp; exit /b %errorlevel%"'
              Directory="INSTALLDIR">
</CustomAction>

<InstallExecuteSequence>
    <Custom Action="TRIGGER_CONFIGURE_AFTER_INSTALL_FINALIZE" After="InstallFinalize" Condition="NOT Installed"></Custom>
</InstallExecuteSequence>

Utilize the CustomAction element with Execute="immediate", Return="asyncNoWait", and Impersonate="no" attributes. The ExeCommand parameter includes a command using cmd.exe to launch the application configure.exe with elevated privileges. The start /wait ensures the installer waits for the process to complete, and exit /b %errorlevel% propagates the application's exit code back to the installer. Although using cmd.exe is a common workaround, alternative methods such as scheduled tasks or a separate launcher executable may be considered based on your specific setup requirements.

However If you needed to run your application with elevated privileges, but you don't want to use Impersonate="false" as it runs the custom action as the SYSTEM user, you can consider using a combination of scheduled tasks and a separate launcher executable. This method allows you to execute the application with elevated privileges without sacrificing the SYSTEM user. Develop a small executable responsible for launching your main application with elevated privileges. You can use a tool like rundll32 or a custom launcher written in a language like C#. In your WiX script, schedule a task to run the launcher executable. Scheduled tasks can be configured to run with elevated privileges.

WiX Custom Action

<CustomAction Id="LaunchApp" Directory="INSTALLDIR" Execute="deferred" Impersonate="no" Return="asyncNoWait" 
              ExeCommand='"[INSTALLDIR]Launcher.exe"' />

InstallExecuteSequence

<InstallExecuteSequence>
    <Custom Action="LaunchApp" After="InstallFinalize" />
</InstallExecuteSequence>

Finally Create a separate executable (e.g., Launcher.exe) that will run your main application with elevated privileges. You can use a tool like ShellExecute in C++ or Process.Start in C# for this purpose

This way avoids running the custom action as the SYSTEM user while still allowing your application to execute with the necessary elevated privileges