Access and edit Shortcut advanced part

Started by smarr, June 14, 2013, 02:55:08 AM

Previous topic - Next topic

smarr

Hi,

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 ?

Thanks in advance

Deana

Quote from: smarr on June 14, 2013, 02:55:08 AM
Hi,

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 ?

Thanks in advance

Unfortunately, the WIL shortcut functions do not support setting 'run as admin'. Its not as simple as it may first appear.

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?

Apparently 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. (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 ( sorry no WIL code sample to handle this ).

The solution I prefer 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.



Deana F.
Technical Support
Wilson WindowWare Inc.

td

Assuming permission requirements are met, this works on Win 7.
Code (winbatch) Select

; Get a path + file name to a link.
strProfile = Environment("USERPROFILE")
strLink    = strProfile:"\desktop\SpeedTest.lnk"
nSize      = FileSize(strLink)

; Load the file
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)
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Deana

Nice Tony! I converted to a UDF and posted to the tech database: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/tsleft.web+WinBatch/Shortcut~Information+Shortcut~Run~As~Administrator.txt

Code (winbatch) Select

;***************************************************************************
;**
;**      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
Deana F.
Technical Support
Wilson WindowWare Inc.

smarr