1

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?

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • possible duplicate of [Make InnoSetup installer request privileges elevation only when needed](http://stackoverflow.com/questions/21556853/make-innosetup-installer-request-privileges-elevation-only-when-needed) – Thomas Levesque Apr 09 '14 at 15:17

1 Answers1

1

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.

Community
  • 1
  • 1
TLama
  • 75,147
  • 17
  • 214
  • 392
  • It worked, thanks! However my question is clearly a duplicate, I should probably close it... – Thomas Levesque Apr 09 '14 at 15:17
  • It wouldn't be duplicate if you would ask why does `ShellExec` fail to run `{srcexe}` which is already part of your question and what I actually answered :-) – TLama Apr 09 '14 at 15:24