WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Shortcut Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Shortcut Run As Administrator

 Keywords: Shortcut Run As RunAs Administrator Admin lnk .lnk ShortcutMake ShortcutExtra ShortcutEdit ShortcutRunAsAdmin Advanced Tab Shell Properties IShellLinkDataList SLDF_RUNAS_USER

Question:

While creating / editing a shortcut to a program, is there a way to access the shortcut advanced part in order to specify the "Run as admin" option ?

Answer:

Unfortunately, the WIL shortcut functions do not support setting 'run as admin'. Its not as simple as it may first appear. Ultimately it is the responsibility of an application to elevate using a manifest. Why do you need to create a shortcut to elevate? Does the application you are trying to launch not have a manifest? Assuming you are trying to run an old application that doesn't have a manifest and it requires admin privileges...

Apparently the recommended method to handle this programmatically is usingthe IShellLinkDataList interface. IShellLinkDataList can be used to mark a shortcut file as requiring elevation. Unfortunately the IShellLinkDataList interface inherits from the IUnknown interface, which rules out COM automation in WinBatch. WinBatch requires an IDispatch interface. Reference: http://msdn.microsoft.com/en-us/library/windows/desktop/bb774916(v=vs.85).aspx

One possible workaround is to flip a bit in the .lnk file. Apparently when you tick the "run as administrator" box in the Advanced tab of the Shell Properties box, or when you use IShellLinkDataList to set the flags to include SLDF_RUNAS_USER, you're basically just setting one bit in the file. It's byte 21, and you need to set the 0x20 bit on. Reference: http://stackoverflow.com/questions/11162802/how-can-i-use-jscript-to-create-a-shortcut-that-uses-run-as-administrator

Here is a UDF that can handle this bit flip.

;***************************************************************************
;** 
;**      Shortcut Run As Administrator
;** 
;** Purpose: Modify a shortcut .lnk file to Run As Administrator
;** Inputs:  Full lnk file path
;** Outputs: 
;** Revisions: Tony D and Deana Falk 2013.06.14
;***************************************************************************
; Apparently when you tick the "run as administrator" box in the Advanced tab of the Shell Properties box, 
; or when you use IShellLinkDataList to set the flags to include SLDF_RUNAS_USER, you're basically just 
; setting one bit in the file.  It's byte 21, and you need to set the 0x20 bit on. 
; Reference: http://stackoverflow.com/questions/11162802/how-can-i-use-jscript-to-create-a-shortcut-that-uses-run-as-administrator ( sorry no WIL code sample to handle this ).

#DefineFunction udfShortcutRunAsAdmin(strLink)
   nSize      = FileSize(strLink)
   ; Load the file into binary buffer
   hLink = BinaryAlloc(nSize + 16)
   BinaryRead(hLink, strLink )
   ; Set the runas bit
   nByte = BinaryPeek(hLink, 21 )
   nByte = nByte | 32
   BinaryPoke(hLink, 21, nByte )
   ; Save the file
   BinaryWrite(hLink, strLink )
   BinaryFree(hLink)
   Return 1
#EndFunction

strProfile = Environment("USERPROFILE")
strLink    = strProfile:"\desktop\cmd_admin.lnk"
ShortCutMake(strLink, 'cmd.exe', '', '', @normal )
udfShortcutRunAsAdmin(strLink)
Exit

Another possible solution is to create a simple WinBatch script which uses ShellExecute with "runas" verb to launch your application as an Administrator. Your shortcut can then point to this wrapper instead of the actual application.


Article ID:   W18278
Filename:   Shortcut Run As Administrator.txt
File Created: 2013:06:14:09:11:54
Last Updated: 2013:06:14:09:11:54