Results 1 to 9 of 9

Thread: [RESOLVED] VB6 QUESTION: Extracting target information from a Windows Shortcut or .LNK

  1. #1

    Thread Starter
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Resolved [RESOLVED] VB6 QUESTION: Extracting target information from a Windows Shortcut or .LNK

    Afternoon chaps.

    I have some code that reads a Windows Shortcut or .LNK in VB6 using the Shell32.Shell object and it works and that's good. However, I am looking for an alternate method to extract the link target details, name, path, arguments &c. The reason I am looking for an alternative is to prevent any false anti-virus tool alerts when calling the shell object (as some a/v tools are known to baulk at this and generate FPs).

    I have found a very good source for what I want to do but it is in VB and C# but of course, not VB6. It looks fairly complicated and I don't feel competent to attempt it myself, at least not without some fairly heavy pointers. So, as usual I am asking if anyone knows of an existing vb6 version or whether they have come across something that might do the job with a bit of coercion.

    The source of the code is here:

    https://www.kittell.net/code/windows...t-lnk-details/

    The link is David Kittel's Blog and is named "Get Windows Shortcut (.lnk) Details" and is also at GitHub at https://github.com/dkittell/lnk-parser

  2. #2

    Thread Starter
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: VB6 QUESTION: Extracting target information from a Windows Shortcut or .LNK

    In the absence of such code already existing. Thinking of the problem, doing it myself, I probably need to read the file into an array as a series of bytes, then search through the array for the various flags and structures and identify them manually using that VB.NET version as a guide. I'll see what I can do.

  3. #3
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    685

    Re: VB6 QUESTION: Extracting target information from a Windows Shortcut or .LNK

    Quote Originally Posted by yereverluvinuncleber View Post
    In the absence of such code already existing. Thinking of the problem, doing it myself, I probably need to read the file into an array as a series of bytes, then search through the array for the various flags and structures and identify them manually using that VB.NET version as a guide. I'll see what I can do.
    Nice, that's right. I found this, not sure if it works, but seems like what you want. https://www.vbforums.com/showthread....ut-information

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: VB6 QUESTION: Extracting target information from a Windows Shortcut or .LNK

    I gave shot at translating the vb.net code you linked to:

    Code:
    Public Function GetShortcutTarget(ShortcutPath As String) As String
    Dim Begin As Long
    Dim EndV As Long
    Dim FileInfoStartsAt As Long
    Dim FileOffset As Long
    Dim FirstPart As String
    Dim Flags As Long
    Dim FileH As Long
    Dim Offset As Integer
    Dim Link As String
    Dim LinkTarget As String
    Dim PathLength As Long
    Dim SecondPart As String
    Dim TotalStructLength As Long
    
       FileH = FreeFile()
       If Dir$(ShortcutPath, vbNormal) = vbNullString Then Error 53
       
       Open ShortcutPath For Binary Lock Read Write As FileH
          Seek #FileH, &H15
          Get #FileH, , Flags
          If (Flags And &H1) = &H1 Then
             Seek #FileH, &H4D
             Get #FileH, , Offset
             Seek #FileH, Seek(FileH) + Offset
          End If
    
          FileInfoStartsAt = Seek(FileH) - 1
          Get #FileH, , TotalStructLength
          Seek #FileH, Seek(FileH) + &HC
          Get #FileH, , FileOffset
          Seek #FileH, FileInfoStartsAt + FileOffset + 1
          
          PathLength = (TotalStructLength + FileInfoStartsAt) - Seek(FileH) - 1
          LinkTarget = Input$(PathLength, FileH)
          Link = LinkTarget
          
          Begin = InStr(Link, vbNullChar & vbNullChar)
          If Begin > 0 Then
             EndV = InStr(Begin + 2, Link, "\\")
             EndV = InStr(EndV, Link, vbNullChar) + 1
           
             FirstPart = Mid$(Link, 1, Begin - 1)
             SecondPart = Mid$(Link, EndV)
     
             GetShortcutTarget = FirstPart & SecondPart
             Exit Function
          End If
    
          GetShortcutTarget = Link
          Exit Function
       Close FileH
    
    GetShortcutTarget = ""
    End Function
    Translating this properly was tricky. Hopefully it works okay for you.

  5. #5

    Thread Starter
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: VB6 QUESTION: Extracting target information from a Windows Shortcut or .LNK

    Bloody Hell! Those two posts were PERFECT. Thanks chaps, Peter and TTn Divine

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 QUESTION: Extracting target information from a Windows Shortcut or .LNK

    Trying to spelunk a persisted IShellLink object's contents is fraught with pitfalls. Is the format even documented? You might get some degenerate cases working but skipper over Shell Item IDs is a big fail for generality.

    If you are getting whacked for using Shell interfaces you might look at other more dubious things in your program that are raising your malware suspicion score. It is pretty hard to write a serious program without Shell interfaces and flat API calls any more.

  7. #7

    Thread Starter
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: VB6 QUESTION: Extracting target information from a Windows Shortcut or .LNK

    Quote Originally Posted by dilettante View Post
    Is the format even documented?
    https://docs.microsoft.com/en-us/ope...d-bf1d6cc0f943

  8. #8
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,647

    Re: VB6 QUESTION: Extracting target information from a Windows Shortcut or .LNK

    You could also look into using IShellLink, and IShellLinkDataList for more advanced info. Should cause less problems than the shell32 object.

    Some shortcuts won't have a path. Some will have environmental variables using IShellLink will resolve for you (or you can retrieve the unresolved version).

    I'm not sure why you would need to parse it manually instead of using those.

  9. #9

    Thread Starter
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: VB6 QUESTION: Extracting target information from a Windows Shortcut or .LNK

    Quote Originally Posted by fafalone View Post
    You could also look into using IShellLink, and IShellLinkDataList
    I'll look into those two recommendations, and yes I had discovered that some shortcuts don't have a path, I was able to resolve that by looking at the relative path and extracting some of the incomplete information from that.

    Quote Originally Posted by fafalone View Post
    I'm not sure why you would need to parse it manually instead of using those.
    Well, I suppose you'd have to know about those two methods first ...

    Secondly, the two code examples provided above to read a shortcut help to explain the shortcut format and how to read it in VB6. It is very useful to have a VB.NET, C# and VB6 equivalent code and an alternative method of achieving the same that I can compare with my own initial primitive methods. It is a help to me to learn new VB6 commands such as SEEK that I didn't even know existed and that knowledge enshrined in the shortcut code can help me enormously in my knowledge of Windows itself and how/why it operates. It is all very well having high level code that encapsulates and provides all that you want to do but it also protects you from knowing what is actually going on. We are back to one of my primary reasons for using VB6, you have dive into the code to get something done.

    - and that is my primary reason for being here, to learn.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width