15

I'm creating an installer using Inno Setup. As part of the install process I'm installing Tomcat. On Windows 7 I suffer from the problem described here:

http://blog.paulbouwer.com/2010/10/23/the-case-of-the-annoying-tomcat-6-monitor/

I can fix it by manually setting the 'Run as administrator' on tomcat7w.exe (the issue and the root cause is the same for tomcat7 as well), but I don't know how to do it through Inno Setup.

I'm finding threads that explain running some_program.exe as administrator, but here the program is started when the Tomcat service starts (e.g. on machine start-up), so I need a way to flag it using Inno Setup to 'Run as administrator' rather than actually run it.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
learnAndImprove
  • 1,339
  • 4
  • 15
  • 25
  • 4
    You can add Registry entry in `[Registry]` Section that will set to run as Administrator as default action for runnint this app. e.g. `Root: "HKLM"; Subkey: "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\"; ValueType: String; ValueName: "{app}\tomcat7w.exe"; ValueData: "RUNASADMIN"; Flags: uninsdeletekeyifempty uninsdeletevalue; MinVersion: 0,6.1` – RobeN Jun 07 '13 at 07:54
  • 1
    possible duplicate of [How to create a shortcut to launch an App with admin privileges from the cmd-line?](http://stackoverflow.com/questions/16083187/how-to-create-a-shortcut-to-launch-an-app-with-admin-privileges-from-the-cmd-lin) – TLama Jun 07 '13 at 08:01

3 Answers3

20

You can add a Registry entry in [Registry] Section that will set run as Administrator as a default action for running this app.

Example:

Root: "HKLM"; Subkey: "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"; \
    ValueType: String; ValueName: "{app}\tomcat7w.exe"; ValueData: "RUNASADMIN"; \
    Flags: uninsdeletekeyifempty uninsdeletevalue; MinVersion: 0,6.1
Ben Aubin
  • 5,542
  • 2
  • 34
  • 54
RobeN
  • 5,346
  • 1
  • 33
  • 50
  • 4
    Compatibility settings are a band-aid, not a proper solution. The proper solution in this case is probably to run it as a service using admin credentials in the first place. – Miral Jun 07 '13 at 21:38
  • @RobeN -- FYI -- just tested this, but adding this to the registry doesn't appear to either set the Run As Administrator checkbox, nor permit an elevated launch when double-clicking on the app. – SMGreenfield Mar 31 '22 at 21:51
  • @SMGreenfield Just tested with Windows 10 and it works well. I do not know if Windows 11 still supports same solution. P.S. Pay attention if the installer is 32 or 64-bit. – RobeN Mar 31 '22 at 22:17
  • @RobeN -- seems if it worked for you on Windows 10, it SHOULD work for Windows 11. I tried setting it on an .exe file that is a 32-bit executable. The installer itself is installing several .exe files, including a 64-bit .exe that can sub-launch that 32-bit .exe. Should it be HKLM or HKA (like all my file associations for the 32-bit app)? "RUNASADMIN" or "~ RUNASADMIN"? – SMGreenfield Apr 01 '22 at 02:35
  • `~` is not needed to my knowledge. The registry entry should go to `HKLM`, but not to `Wow6432Node`. There is an easy way to check if you enter it correct way. Please set Admin Rights manually for the Executable and then go to Registry to check what entry has been added. Then check if the result of the installer is same. – RobeN Apr 02 '22 at 11:13
  • on Windows10 when i set the flag in the lnk file and check the registry entry it adds the ~. Also attention, there is a space after ~ and before RUNASADMIN – Kostas Markakis Oct 31 '22 at 08:40
16

If you really want to set the "Run as administrator" flag of the shortcut (as opposite to forcing the target application run with administrator privileges), you can use this code:

[Icons]
Name: "{userdesktop}\My Program"; Filename: "{app}\MyProg.exe"; \
  AfterInstall: SetElevationBit('{userdesktop}\My Program.lnk')
[Code]

procedure SetElevationBit(Filename: string);
var
  Buffer: string;
  Stream: TStream;
begin
  Filename := ExpandConstant(Filename);
  Log('Setting elevation bit for ' + Filename);

  Stream := TFileStream.Create(FileName, fmOpenReadWrite);
  try
    Stream.Seek(21, soFromBeginning);
    SetLength(Buffer, 1);
    Stream.ReadBuffer(Buffer, 1);
    Buffer[1] := Chr(Ord(Buffer[1]) or $20);
    Stream.Seek(-1, soFromCurrent);
    Stream.WriteBuffer(Buffer, 1);
  finally
    Stream.Free;
  end;
end;

This is based on:


Tested on Unicode version of Inno Setup. But it should, even more naturally, work on Ansi version too, though you should use Unicode version anyway.


If you want to allow user to execute the program at the end of the installation using a postinstall entry in [Run] section, you will of course need to explicitly request the elevation.

If the installer runs with Administrator privileges, you can simply add runascurrentuser flag:

[Run]
Filename: "{app}\MyProg.exe"; Description: "Launch application"; \
    Flags: postinstall nowait skipifsilent runascurrentuser 

If the installer runs without Administrator privileges, set Verb parameter to runas (for that you also need shellexec flag):

[Run]
Filename: "{app}\MyProg.exe"; Verb: runas; Description: "Launch application"; \
    Flags: postinstall nowait skipifsilent shellexec

Though, make sure you have a very good reason to run your application with Administrator privileges. User applications should not need Administrator privileges. If they need it, it's usually a sign of a bad design. One common (bad) reason to want an application to run with Administrator privileges, is that the application needs to write to its installation folder.

See Application does not work when installed with Inno Setup

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    For Inno Devs: If the postinstall entry in [Run] section is actually another Inno Setup executable, define a flag to capture the return code of the (failing) child process with option to XAND it to the.return code of the parent install. – Laurie Stearn Jul 17 '17 at 07:15
  • 2
    @LaurieStearn Why do you post a feature request here? Post it on Inno Setup site. – Martin Prikryl Jul 17 '17 at 07:17
  • 2
    [Yes, of course](http://news.jrsoftware.org/read/article.php?id=105253&group=jrsoftware.innosetup#105253). Should be XOR anyhow. A workaround for progress [here](https://stackoverflow.com/questions/40947436/inno-setup-make-inno-setup-installer-report-its-installation-progress-status-t). – Laurie Stearn Jul 17 '17 at 14:56
4

Add the runascurrentuser flag attribute to the [Run] section

Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: runascurrentuser nowait postinstall skipifsilent; 
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Mehdi Benkirane
  • 437
  • 4
  • 7
  • 5
    Will launch the executable as administrator properly at the end of the setup but won't make the desktop's shortcut launch it as administrator. – Winter Jun 15 '17 at 13:53
  • @Winter I added `runascurrentuser` as per answer and it asked me for permissions when executing the application from the shortcut. `Inno Setup 6.0.2` – traveler3468 May 17 '20 at 13:01