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 && exit 0" Directory="INSTALLDIR"></CustomAction>
<InstallExecuteSequence>
<Custom Action="TRIGGER_CONFIGURE_AFTER_INSTALL_FINALIZE" After="InstallFinalize" Condition="NOT Installed"></Custom>
</InstallExecuteSequence>
To start a GUI application as an elevated user after the installation finalizes in a WiX setup, you can use the
CustomAction
element withExeCommand
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:
Utilize the
CustomAction
element withExecute="immediate", Return="asyncNoWait"
, andImpersonate="no"
attributes. TheExeCommand
parameter includes a command usingcmd.exe
to launch the applicationconfigure.exe
with elevated privileges. Thestart /wait
ensures the installer waits for the process to complete, andexit /b %errorlevel%
propagates the application's exit code back to the installer. Although usingcmd.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 likerundll32
or a custom launcher written in a language likeC#
. 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
InstallExecuteSequence
Finally Create a separate executable
(e.g., Launcher.exe)
that will run your main application with elevated privileges. You can use a tool likeShellExecute
in C++ orProcess.Start
in C# for this purposeThis way avoids running the custom action as the SYSTEM user while still allowing your application to execute with the necessary elevated privileges