Modify byte in shortcut .lnk file to set flag Run as Administrator ON Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cmaggic
Posts: 2
Joined: 03 Jan 2020, 16:13

Modify byte in shortcut .lnk file to set flag Run as Administrator ON

03 Jan 2020, 16:27

Hi, I want to create a shortcut with the "Run as Administrator" flag ON.
I can do it with powershell in this way (modifying the byte at pos 22):

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
function SetAsAdministrator ($p_LinkFile) {
 $tempFileName = [IO.Path]::GetRandomFileName()
 $tempFile = [IO.FileInfo][IO.Path]::Combine($p_LinkFile.Directory, $tempFileName)
 $writer = new-object System.IO.FileStream $tempFile, ([System.IO.FileMode]::Create)
 $reader = $LinkFile.OpenRead()		
 while ($reader.Position -lt $reader.Length)
	{		
		$byte = $reader.ReadByte()
		if ($reader.Position -eq 22) {
			$byte = 34
		}
		$writer.WriteByte($byte)            
	}		
 $reader.Close()
 $writer.Close()
 $LinkFile.Delete()
 Rename-Item -Path $tempFile -NewName $p_LinkFile.Name
}

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;What I Want is to do the same with Autohotkey but i has no success using something like this:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FileCreateShortcut, Notepad.exe, %A_Desktop%\My Shortcut1.lnk, C:\, "%A_ScriptFullPath%", My Description, C:\My Icon.ico, i
MyFile1 = %A_Desktop%\My Shortcut1.lnk
	FileHnd1 := FileOpen(MyFile1, "rw")
			if !IsObject(FileHnd1)
			{
				MsgBox Can't open "%MyFile1%" for writing.
				return
			}
			FileHnd1.Position(22)
			MyVar :=  34
			FileHnd.RawWrite(MyVar,1)
			FileHnd1.Close()
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Mod Edit: [code][/code] tags!
Regards.
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Modify byte in shortcut .lnk file to set flag Run as Administrator ON  Topic is solved

04 Jan 2020, 07:01

I also needed this so after some class hackery, looks like the the magic flag offset is 21 lol

Code: Select all

; Path = **path to a shortcut with .lnk extension** 

ShortcutAdmin.Read( Path )	; Returns File Object on success, False on Error
ShortcutAdmin.On			; Run As Administrator On
; ShortcutAdmin.Off 		; Run As Administrator Off

class ShortcutAdmin {
	Read( path, flags := "rw", codePage := "CP1252" ) {
		if !FileExist( path ) || (ext:=StrSplit( path, "."))[ext.length()] != "lnk" {
			MsgBox, 0x10, Error!, Either the Shortcut is missing`nor there's a problem with it!
			return false			
		}
		return this.scObj := FileOpen( path, flags, codepage )
	}

	Mode( bool, caller ) {
		if ( InStr( caller, "ShortcutAdmin.O" ) = true && ( bool = true || bool = false ) ) {
			VarSetCapacity( sHCt, 2 ), this.scObj.RawRead( &sHCt, this.scObj.Length() )
			StrPut( Chr( bool = true ? 32 : 0 ), &sHCt+21, 1, this.scObj.Encoding )
			this.scObj.Pos := 0, this.scObj.RawWrite( &sHCt, this.scObj.Length() )
			this.scObj.Close()
			return true
		}
		return false
	}
	
	On {
		Get {
			this.Mode( true, A_ThisFunc )
		}
	}

	Off {
		Get {
			this.Mode( false, A_ThisFunc )
		}
	}
}
Give it a test on a backed up lnk. lemmi know how it works out for ya.
I may scrunch it down on too btw ;)
cmaggic
Posts: 2
Joined: 03 Jan 2020, 16:13

Re: Modify byte in shortcut .lnk file to set flag Run as Administrator ON

04 Jan 2020, 07:32

TLM wrote:
04 Jan 2020, 07:01
I also needed this so after some class hackery, looks like the the magic flag offset is 21 lol

Code: Select all

; Path = **path to a shortcut with .lnk extension** 

ShortcutAdmin.Read( Path )	; Returns File Object on success, False on Error
ShortcutAdmin.On			; Run As Administrator On
; ShortcutAdmin.Off 		; Run As Administrator Off

class ShortcutAdmin {
	Read( path, flags := "rw", codePage := "CP1252" ) {
		if !FileExist( path ) || (ext:=StrSplit( path, "."))[ext.length()] != "lnk" {
			MsgBox, 0x10, Error!, Either the Shortcut is missing`nor there's a problem with it!
			return false			
		}
		return this.scObj := FileOpen( path, flags, codepage )
	}

	Mode( bool, caller ) {
		if ( InStr( caller, "ShortcutAdmin.O" ) = true && ( bool = true || bool = false ) ) {
			VarSetCapacity( sHCt, 2 ), this.scObj.RawRead( sHCt, this.scObj.Length() )
			StrPut( Chr( bool = true ? 32 : 0 ), &sHCt+21, 1, this.scObj.Encoding )
			this.scObj.Pos := 0, this.scObj.RawWrite( &sHCt, this.scObj.Length() )
			this.scObj.Close()
			return true
		}
		return false
	}
	
	On {
		Get {
			this.Mode( true, A_ThisFunc )
		}
	}

	Off {
		Get {
			this.Mode( false, A_ThisFunc )
		}
	}
}
Give it a test on a backed up lnk. lemmi know how it works out for ya.
I may scrunch it down on too btw ;)
Thank you TLM it worked perfectly !!
:bravo:
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: Modify byte in shortcut .lnk file to set flag Run as Administrator ON

04 Jan 2020, 07:59

awez :D
literally 1 optional character you can change VarSetCapacity( sHCt, 2 ), this.scObj.RawRead( &sHCt, this.scObj.Length() )
:lol:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Joey5 and 162 guests