I have an InnoSetup installer that can usually run as non-admin, but in some cases it needs to run with elevated privileges if a condition is met. So I set PrivilegesRequired
to lowest
to ensure it doesn't show the UAC prompt when not required, and if the condition is met, I try to restart it as admin like this:
if not ShellExec('runas', ExpandConstant('{srcexe}'), GetCmdTail(), '', SW_HIDE, ewNoWait, errorCode) then begin
MsgBox(SysErrorMessage(errorCode), mbError, MB_OK);
end;
But it always fail with error 5 : access denied.
Apparently it's not because I can't use the runas
verb: running another executable with that verb works fine, and shows the UAC prompt. I also tried copying setup.exe to the temp directory and running it from there, but the copy fails.
How can I restart my setup as admin?
It is the limitation of the ShellExec
function. It explicitly compares
if the file that you're going to execute is not the installer itself and if so, it fails
with ERROR_ACCESS_DENIED
. So, you just can't run the installer with the ShellExec
function.
In this post
I've had to use ShellExecute
Windows API function to workaround this limit, and you can do the same.