WinBatch Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: smarr on June 14, 2013, 02:55:08 am

Title: Access and edit Shortcut advanced part
Post by: 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
Title: Re: Access and edit Shortcut advanced part
Post by: Deana on June 14, 2013, 08:16:24 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.



Title: Re: Access and edit Shortcut advanced part
Post by: td on June 14, 2013, 08:43:51 am
Assuming permission requirements are met, this works on Win 7.
Code: Winbatch
; 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)
 
Title: Re: Access and edit Shortcut advanced part
Post by: Deana on June 14, 2013, 09:14:31 am
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
;***************************************************************************
;**
;**      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
Title: Re: Access and edit Shortcut advanced part
Post by: smarr on June 17, 2013, 06:08:15 am
Works fine, Thanks a lot