How to create a shortcut to launch an App with admin privileges from the cmd-line?

后端 未结 4 614
死守一世寂寞
死守一世寂寞 2021-01-02 06:19

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 ap

相关标签:
4条回答
  • 2021-01-02 06:41

    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.

    0 讨论(0)
  • 2021-01-02 06:46

    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
    
    0 讨论(0)
  • 2021-01-02 06:48

    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 讨论(0)
  • 2021-01-02 06:50

    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.

    0 讨论(0)
提交回复
热议问题