Jump to content

Recommended Posts

Posted

objective: get url from *.lnk files that were created with FileCreateShortcut(). just found out been creating lnk wrong but when double click on it it does open up in the default browser. the only problem is it fails to retrieve url with FileGetShortcut(). there are lots of shortcuts that need to retrieve url from and wondering if there is any chance of an alternative way to get it? any thoughts would be appreciated, thanks, also see screenshots

bellow is the function used to demonstrate both the wrong way of create lnk and the correct way.

_Create_LNK_Shortcut('https://www.autoitscript.com/forum/', @DesktopDir)

Func _Create_LNK_Shortcut($Url, $sFilePath)

    Local $LnkFilePath = $sFilePath                                                 ;Location & lnk name to saved to
    Local $Browser = @ProgramFilesDir & '\Internet Explorer\iexplore.exe'
    Local $Browser = @ProgramFilesDir & '\Google\Chrome\Application\chrome.exe'     ;Part1 of Target:        
    ;Local $Url = $sUrl                                                             ;Argument option Part 2 of Target for URL or options for EXE
    Local $Icon = $Browser                                                          ;lnk icon from exe 
    Local $IconIndex = 0
    Local $StartIn = @ProgramFilesDir & '\Google\Chrome\Application\'
    Local $StartIn = @ProgramFilesDir & '\Google\Chrome\Application\'
    
    
    Local $Comment = @YEAR&@MON&@MDAY&'_'&@HOUR&@MIN&@SEC&'.'&@MSEC

    ; wrong way to create *lnk
    FileCreateShortcut($Url, $LnkFilePath &'_wrong.lnk', $StartIn, "", $Comment, $Icon, '', $IconIndex, @SW_SHOWMAXIMIZED)

    ; correct way to create *lnk
    FileCreateShortcut($Browser, $LnkFilePath &'_correct.lnk', $StartIn, $Url, $Comment, $Icon, '', $IconIndex, @SW_SHOWMAXIMIZED)

    ; Retrieve details about the shortcut.
    Local $aDetails_wrong = FileGetShortcut($LnkFilePath &'_wrong.lnk')
    Local $aDetails_correct = FileGetShortcut($LnkFilePath &'_correct.lnk')
    
    If Not @error Then
        MsgBox($MB_SYSTEMMODAL, "Compare lnks", _
                '___WRONG LNK___' & @CRLF & _
                "Browser: " & $aDetails_wrong[0] & @CRLF & _
                "StartIn: " & $aDetails_wrong[1] & @CRLF & _
                "Url: " & $aDetails_wrong[2] & @CRLF & _
                "Comment: " & $aDetails_wrong[3] & @CRLF & _
                "Icon: " & $aDetails_wrong[4] & @CRLF & _
                "Icon id: " & $aDetails_wrong[5] & @CRLF & _
                "lnk state: " & $aDetails_wrong[6] & @CRLF & _
                ''&@CRLF & _
                '___CORRECT LNK___' & @CRLF & _
                "Path: " & $aDetails_correct[0] & @CRLF & _
                "StartIn: " & $aDetails_correct[1] & @CRLF & _
                "Url: " & $aDetails_correct[2] & @CRLF & _
                "Comment: " & $aDetails_correct[3] & @CRLF & _
                "Icon: " & $aDetails_correct[4] & @CRLF & _
                "Icon id: " & $aDetails_correct[5] & @CRLF & _
                "lnk state: " & $aDetails_correct[6] & @CRLF)
    EndIf
EndFunc

 

create_shortcut_lnk.png

Posted

tried reading *.lnk as text to then extract url with regexp but it only read in first letter 'L', any idea why

Local $sFilePath = @DesktopDir &'\_wrong.lnk'
      Local $hFileOpen = FileOpen($sFilePath, $FO_BINARY)
      If $hFileOpen Then
          Local $sFileRead = FileRead($hFileOpen)
          FileClose($hFileOpen)
          
          MsgBox(0, 'lnk', BinaryToString($sFileRead, 4))
          ;MsgBox(0, 'lnk in binary', $sFileRead)
      EndIf

 

read_lnk_as_text.png

_wrong.zipFetching info...

Posted (edited)
  On 9/19/2020 at 6:42 AM, jerem488 said:
Expand  

It was not elegant, to say the least ;)

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
$Shortcut = @ScriptDir & '\MyShortcut.lnk'
$Browser = @ProgramFilesDir & '\Internet Explorer\iexplore.exe'
$Url = 'http://www.autoitscript.com/autoit3'
$Icon = @SystemDir & '\shell32.dll'
$IconIndex = 13

FileCreateShortcut($Browser, $Shortcut, '', $Url, '', $Icon, '', $IconIndex, @SW_SHOWMAXIMIZED)

And if you want use another browser you can use the _WinAPI_FindExecutable() function to find your default browser.

Qui ose gagneWho Dares Win[left]CyberExploit[/left]

Posted
  On 9/19/2020 at 6:49 AM, jerem488 said:

And if you want use another browser you can use the _WinAPI_FindExecutable() function to find your default browser.

Expand  

again, i'm trying to extract url from the shortcuts which i have already created incorrectly (:

Posted

Are there many of them? Manual extraction of the path would be a way.

  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted
  On 9/19/2020 at 11:02 AM, careca said:

Are there many of them? Manual extraction of the path would be a way.

Expand  

yea only a few thousands of these. as screenshot above demonstrated, manual extraction is not possible because field is disable plus is not the route i want to go through lol.

could someone confirm why FileRead() can't read *.lnk as text but only able to read only the 1st letter 'L'. example shortcut '_wrong.lnk' file attached above. if shortcut could be read in as text show in notepad i could StringRegex() the url from that

Posted

Try this :

Local $hFileOpen = FileOpen($sFilePath & '_wrong.lnk', $FO_BINARY)
      If $hFileOpen Then
          Local $sFileRead = FileRead($hFileOpen)
          FileClose($hFileOpen)
          MsgBox ($MB_SYSTEMMODAL,"",BinaryToString(BinaryMid($sFileRead,107,80),2))

        For $i = 1 to BinaryLen ($sFileRead)
          ConsoleWrite ($i & " = " & BinaryMid($sFileRead,$i,1) & @CRLF)
        Next
      EndIf

 

Posted

This works for me although it is not a failsafe approach.

 

#include <Constants.au3>

Local $filePath = @DesktopDir &'\_wrong.lnk'

Local $fileHandle = FileOpen($filePath, $FO_ANSI)
 If Not @error Then
   Local $contents = StringRegExpReplace(FileRead($fileHandle), "\x00", "")
   Local $matches = StringRegExp($contents, "https?[\w:/.]*", $STR_REGEXPARRAYMATCH)
   msgbox($MB_SYSTEMMODAL, "", $matches[0])

   FileClose($fileHandle)
Else
   ConsoleWrite('Could not open file "' & $filePath & '".')
   Exit 1
EndIf

 

Posted
  On 9/19/2020 at 12:22 PM, Danp2 said:

Have you tried supplying the optional count parameter?

Expand  

it looks like it fully read in the whole file but fails at BinaryToString(). suggested examples above by @boom221 to regex replace '\x00' and @Nine range out with BinaryMid() did the trick

For $i = 1 to BinaryLen ($sFileRead)
          ConsoleWrite ($i & " = " & BinaryMid($sFileRead,$i,1) & @CRLF)
        Next

@Nine very use for future testing approach, thanks

@boom221 exactly what i'm looking for, thanks

thanks again everyone, here is the alternative to FileGetShortcut() to only extract url from a corrupted or badly formatted shortcut

Func _FileGetShortcutUrl($sFilePath)
        Local $sUrlMatch = ''
        ;Local $sFilePath = @DesktopDir &'\_wrong.lnk'
        Local $hFileOpen = FileOpen($sFilePath, $FO_ANSI) ;opt: $FO_UNICODE, $FO_UTF8
        Local $iSizeCheck = FileGetSize($sFilePath)
        If $hFileOpen And $iSizeCheck Then 
            Local $sFileRead = FileRead($hFileOpen)
                  $sFileRead = StringRegExpReplace($sFileRead, "\x00", "")
            Local $regex_url = "https?[\w:/.]*"
                  $sUrlMatch = StringRegExp($sFileRead, $regex_url, $STR_REGEXPARRAYMATCH)[0]
            
            MsgBox(0, @ScriptLineNumber&': lnk ', 'lnk: '&$sFileRead &@CRLF&@CRLF&'url: '&$sUrlMatch)
        Else
            MsgBox(0, @ScriptLineNumber&': lnk', 'Failed $hFileOpen or $iSizeCheck')
        EndIf
        FileClose($hFileOpen)
        Return $sUrlMatch
    EndFunc

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...