10

I have an installer (Inno-Setup) that installs my application to a path defined by the user. At the end of the install routine i want to create a shortcut that starts the application with admin privileges. The solution should work on all win version from winXP to Win7.

What can i do to achieve this?

I know that it is possible with a batch script, that executes a nasty vb-script. The disadvantage is that the cmd-window popup and it only works on win7 i guess.

I also tried the command mklink to create a hyperlink, but it does not work because it is not possible to pass an argument that set the admin priviliges.

Community
  • 1
  • 1
Chriss
  • 5,157
  • 7
  • 41
  • 75
  • Add `runas` before the command line. – TLama Apr 18 '13 at 12:41
  • Where do you want to create the hyperlink? Inno uses standard Win32 controls for which the native method is a button, or a check box on the final page. Or do you want to create a shortcut that the user can run later? – Deanna Apr 18 '13 at 12:51
  • @TLama `runas /user:admin` prompt a password on the cmd-line that not an option the user should get the dialog prompt – Chriss Apr 18 '13 at 13:06
  • @Deanna I want to create a shortcut that the user can run later to launch the app – Chriss Apr 18 '13 at 13:07
  • 1
    @Chriss Then please see [my answer](http://stackoverflow.com/a/16083426/588306) below. – Deanna Apr 18 '13 at 13:08
  • 2
    @Chriss Also check your terminology. A hyperlink is a completely different concept. Symlinks (via `mklink`) are different again. You're asking about a shortcut (a `.lnk` file). – Deanna Apr 18 '13 at 13:10
  • thanks for clarification i changed the terms – Chriss Apr 18 '13 at 13:25

4 Answers4

12

You can add a registry-key that tells windows to execute your program as admin:

Under HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers, just add a key(REG_SZ) <Path to your exe> with the value RUNASADMIN. When you launch your exe, you will be prompted for admin-access.

With that, you can simply create a normal shortcut to your executable like you would do it with Inno-Setup.

If you want to do so via a cmd or a batch-file, you can use the following command:

reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "<Path to your exe>" /t REG_SZ /d RUNASADMIN
looper
  • 1,929
  • 23
  • 42
  • But how to do that using a batch-file? Can you provide a sample? – Chriss Apr 18 '13 at 13:49
  • Nice and easy! It tied it on Win7 and it works. I will add this to the end of the installer script and done! Every time the user starts now the app the permission dialog show up. – Chriss Apr 18 '13 at 13:54
  • 1
    Downvoted as it's a compatability hack, and not designed for new applications. The key itself is not fully documented and can be [turned off entirely](http://msdn.microsoft.com/en-us/library/windows/desktop/bb756937.aspx). The correct way to do this is via the manifest for Vista+ and `runas` on pre Vista. – Deanna Apr 18 '13 at 14:28
  • This will also effect anything else using the same executable. e.g. `java.exe` will cause it to try and run all java apps as admin. – Deanna Apr 18 '13 at 14:35
  • does not work on windows 10 – Kostas Markakis Jan 30 '23 at 14:41
1

The "Run as admin" is a property of the executable, not the shortcut. You should add the required manifest that makes Windows prompt for elevation.

To do this on Windows XP, you will need to use the runas verb with ShellExecute() to run as a different user, but this will remove any ability to access the local profile. This can be done from your exe when it finds it's not running with full admin access.

Community
  • 1
  • 1
Deanna
  • 23,876
  • 7
  • 71
  • 156
  • the problem is that i cant change or recompile the exe, its a java application, i also tried the `runas /user:admin` command but it doesnt show a dialog promt but a command line password promt – Chriss Apr 18 '13 at 13:10
  • Yes, XP has no concept of UAC or elevation. If the user does not have admin access, your only option is to run as another user that does. – Deanna Apr 18 '13 at 14:29
  • I've also adjusted my answer, runas is CLI only, you need to use the `runas` verb to `ShellExecute()` instead – Deanna Apr 18 '13 at 14:33
  • I don't know what arguments runas should use and were the ShelExecute() belongs to, it doesn't look like a cmd-command. Can you provide a sample, that works for XP and win7? – Chriss Apr 19 '13 at 12:25
  • `ShellExecute()` is a Win32 API function, that you call from your application. One of the parameters is `verb` which you pass `runas` to. There are plenty of samples online, but exact usage depends on what language your code is in. – Deanna Apr 19 '13 at 13:15
  • 4
    There is a "Run as administrator" property for shortcuts (under "Advanced" in the GUI). It should be possible to create a shortcut with this property set from Powershell or VBScript, I suppose, though I would have thought Inno Setup had a built-in mechanism for manipulating shortcuts. – Harry Johnston Apr 26 '13 at 01:19
  • 1
    @Deanna: no, it definitely changes the shortcut. I just created two shortcuts to cmd.exe on a network drive, and set the "run as administrator" flag on one of them. This change worked on the one shortcut as expected but did not affect the other one. Also, opening the flagged shortcut from another computer resulted in elevation, so the flag can't have been stored in the registry. (Windows 7.) – Harry Johnston Apr 27 '13 at 04:49
0

After creating the shortcut, change its 21st byte (position 0x15) to 32 (0x20) to make it "Run as Administrator". Changing it back to 0 makes it a "normal" (non-admin) shortcut.

0

Can be done shortcutjs.bat:

shortcutjs.bat -linkfile tst6.lnk -target "%cd%\myscript.bat" -adminpermissions yes

-adminpermissions yes is for if you want to run the bat as administrator. You'll need the full path to your script.

The 'run as admin' tick sets a binary flag in the .lnk file (21st character) and this what the script is doing too - reads it as binary steam and changes that value.

npocmaka
  • 55,367
  • 18
  • 148
  • 187