Results 1 to 18 of 18

Thread: [RESOLVED] How to get the "Target" from a shortcut file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Resolved [RESOLVED] How to get the "Target" from a shortcut file

    Is there an easy way to get the "Target" from a shortcut (*.lnk) file?

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How to get the "Target" from a shortcut file

    Probably by either using the Windows Script Host Object Model or the Shell Controls and Automation Library. Link resolution can be a dynamic process though and trying to access the Target can produce dialogs, invoke Windows Installer, etc.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: How to get the "Target" from a shortcut file

    How do I reference the ''Windows Script Host Object'', do you know?
    I looked in references, can't see it.
    Last edited by 5ms?; Jun 4th, 2010 at 11:51 AM. Reason: Can't see it

  4. #4
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: How to get the "Target" from a shortcut file

    menu Project > References.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: How to get the "Target" from a shortcut file

    Can't see it, wot dais it look like?

  6. #6
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: How to get the "Target" from a shortcut file

    From the menu bar click Project then reference. Select microsoft Shell Controls And Automation from the list. If its not there, click browse and select shell32.dll from Windows\System32 folder.

    In this example, there is an image at "D:\BG.jpg". The shortcut is at "D:\Shortcut to BG.jpg.lnk".

    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim oSH32 As Shell, oItem As Shell32.FolderItem, oFldr As Shell32.Folder
    5.    
    6.     Set oSH32 = New Shell
    7.     Set oFldr = oSH32.NameSpace("D:\")
    8.     Set oItem = oFldr.ParseName("Shortcut to BG.jpg.lnk")
    9.    
    10.     Debug.Print "Shortcut Path: " + oItem.Path
    11.     Debug.Print "Shortcut Target: " + oItem.GetLink.Path
    12.    
    13.     Set oSH32 = Nothing
    14.     Set oFldr = Nothing
    15.     Set oItem = Nothing
    16.    
    17.     Call Unload(Me)
    18. End Sub

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to get the "Target" from a shortcut file

    This worked for me under XP, no refs needed...

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Debug.Print GetTarget("C:\Test.Lnk")
    End Sub
    
    Function GetTarget(ByVal FileName As String) As String
        Dim Obj As Object, Shortcut As Object
        Set Obj = CreateObject("WScript.Shell")
        Set Shortcut = Obj.CreateShortcut(FileName)
        GetTarget = Shortcut.TargetPath
        Set Obj = Nothing
        Set Shortcut = Nothing
    End Function
    Last edited by Edgemeal; Jun 4th, 2010 at 12:07 PM.

  8. #8
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: How to get the "Target" from a shortcut file

    It says "Windows Script Host Object Model" in the list.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: How to get the "Target" from a shortcut file

    Yes........!!!!!!!!!!

    It's easy when you know wot to look for.

    Thanks guys!

    Have a nice day,
    Regards 5ms.

  10. #10
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] How to get the "Target" from a shortcut file

    I think I found one caveat recently.

    The library "Microsoft Shell Controls and Automation" did not maintain binary compatibility in all newer versions of Windows for some reason (Microsoft goofed?). So if you use early binding your program created on Windows XP probably won't work on Windows 7 and vice versa. I don't recall where Vista fell in this, but it probably also has the issue.

    This is a lot like what you run into when automating Office programs of different versions.

    I saw the same sort of problem with ADOX. Initially in Vista there was only an ADOX 6.0 type library, which meant that WinXP programs would fail to run under Vista. In Vista SP2 this was corrected and a typelib for ADOX 2.8 was added (msadox28.tlb) along with adding the ADOX 2.8 interfaces to msadox.dll.


    I now try to avoid early binding for any of these things in my programs.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: [RESOLVED] How to get the "Target" from a shortcut file

    Xiphias3 code, got the job dun.

    baja_yu, can't see it, Please! post the browse and select name of the .dll for the "Windows Script Host Object Model"

    Edgemeal, I'm under XP sp1, your code I got this,
    Attached Images Attached Images  

  12. #12
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: [RESOLVED] How to get the "Target" from a shortcut file


  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: [RESOLVED] How to get the "Target" from a shortcut file

    OK, I see it's the wshom.ocx, Thank you baja_yu

    baja_yu, I can't Rate your post, so Thank you will have to do.

    Have a nice day,
    Regards 5ms.
    Last edited by 5ms?; Jun 4th, 2010 at 02:07 PM.

  14. #14
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: [RESOLVED] How to get the "Target" from a shortcut file

    No problem!

  15. #15
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] How to get the "Target" from a shortcut file

    You can also late-bind to Shell32.dll as in:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Const ssfDRIVES = 17 'My Computer.  Virtual folder containing
                             'everything on the local computer: storage
                             'devices, printers, and Control Panel. This
                             'folder may also contain mapped network drives.
        
        AutoRedraw = True
        Show
        
        With CreateObject("Shell.Application").NameSpace(ssfDRIVES). _
                ParseName("D:\ZZtestZZ\SomeRandom.lnk")
            Print "Shortcut Path: "; .Path
            Print "Shortcut Target: "; .GetLink.Path
        End With
    End Sub
    Note the use of ssfDRIVES which avoids the need to parse off the folder name first if you have a full pathname to the shortcut file.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: [RESOLVED] How to get the "Target" from a shortcut file

    Quote Originally Posted by dilettante View Post
    You can also late-bind to Shell32.dll as in:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Const ssfDRIVES = 17 'My Computer.  Virtual folder containing
                             'everything on the local computer: storage
                             'devices, printers, and Control Panel. This
                             'folder may also contain mapped network drives.
        
        AutoRedraw = True
        Show
        
        With CreateObject("Shell.Application").NameSpace(ssfDRIVES). _
                ParseName("D:\ZZtestZZ\SomeRandom.lnk")
            Print "Shortcut Path: "; .Path
            Print "Shortcut Target: "; .GetLink.Path
        End With
    End Sub
    Note the use of ssfDRIVES which avoids the need to parse off the folder name first if you have a full pathname to the shortcut file.
    Would I need to "Set CreateObject = Nothing"

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Location
    Lost in thought
    Posts
    349

    Re: [RESOLVED] How to get the "Target" from a shortcut file

    I'll be calling the ''GetTarget2'' about 20 times in a row, there is no references, so the Created Object will be destroyed on ''End Function'', yes/no?
    Code:
    Function GetTarget2(ByVal FileName As String) As String
    
        With CreateObject("Shell.Application").NameSpace(17).ParseName(FileName)
            GetTarget2 = .GetLink.Path
        End With
    
    End Function

  18. #18
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] How to get the "Target" from a shortcut file

    If you need to fetch several of these you can reduce the overhead by not re-creating the objects repeatedly:
    Code:
    Option Explicit
    
    Private Const ssfDRIVES = 17 'My Computer.  Virtual folder containing
                                 'everything on the local computer: storage
                                 'devices, printers, and Control Panel. This
                                 'folder may also contain mapped network drives.
    
    Private MyComputer As Object
    
    Private Function GetTarget(ByVal LinkPath As String) As String
        GetTarget = MyComputer.ParseName(LinkPath).GetLink.Path
    End Function
    
    Private Sub Form_Load()
        Set MyComputer = CreateObject("Shell.Application").NameSpace(ssfDRIVES)
        
        AutoRedraw = True
        Show
        
        Print GetTarget("D:\SomeShortcuts\Notepad.lnk")
        Print GetTarget("D:\SomeShortcuts\WMPlayer.lnk")
    End Sub
    In this case MyComputer will release its reference when the Form unloads. If you had a reason to release it early (done with it but lots more to do in the program) just set it to Nothing.


    It would probably be cleaner to bundle all of this in a Class.

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