Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Windows Shortcut Creation

by gwhite (Friar)
on Jul 26, 2005 at 15:39 UTC ( #478252=perlquestion: print w/replies, xml ) Need Help??

gwhite has asked for the wisdom of the Perl Monks concerning the following question:

I have created a shortcut to a folder on a Windows XP Pro system using Win32::Shortcut. When I click on the shortcut, it asks which application I want to open the shortcut in, which is incorrect functionality. If I open the properties of the shortcut and change any single item (delete one character from comment, change icon, change screen mode), and apply the change the shortcut will now work as it should, opening the directory. I created a shortcut to the same directory manually and compared the elements side by side (using Win32::Shortcut and loading in the values) and nothing other than size is different. System generated shortcut is 1kb and the Perl one is 2kb.

Is this a known issue? Any workarounds?


Replies are listed 'Best First'.
Re: Windows Shortcut Creation
by puploki (Hermit) on Jul 26, 2005 at 15:55 UTC
    Could you post the code you're using?

    I'm trying to fathom out the syntax of Win32::Shortcut, but the documentation isn't great and I can't even get it to generate a shortcut with which to test!


      I found some documentation for this module. I don't know why it's not in the default POD.

      - j

      Guys this is a little frustrating and will get me down voted, but I have stated I can create a shortcut. It is to a directory not a file, and if I open the properties of the Perl created shortcut and make any change then it will work. That said, here is the code:

      $link = Win32::Shortcut->new(); $link->{'File'} = '"' . $archive_drive_path . $disc_path . 'Projec +ts Folder.lnk"'; $link->{'Path'} = '"' . $master_project_path . '"'; $link->{'Description'} = 'RDI Projects Folder'; $link->{'ShowCmd'} = 1; $link->{'IconLocation'} = 'C:\WINNT\system32\SHELL32.dll'; $link->{'IconNumber'} = '9'; $link->Save( $archive_drive_path . $_ . '/Projects Folder.lnk');

      I have the documentation, the shortcut creates, it just doesn't work until I manually edit it. Like I said previously, all I have to do is add or delete a character from the comment line, or change the icon and apply the change then the shortcut works.

      Is this a known issue? Any workarounds? (note: ikegami's solution may get the job done.)


        Whoa! You're not creating a shortcut to a directory, you're creating a shortcut to a shortcut (Projects Folder.lnk)! That's probably why it doesn't work.

Re: Windows Shortcut Creation
by jimbojones (Friar) on Jul 26, 2005 at 16:38 UTC

    The following works for me on a WinXP Pro box:
    use Win32::Shortcut; use strict; use warnings; my $link = Win32::Shortcut->new(); $link->{'Path'} = 'C:\Documents and Settings\jim\My Documents\google_m +aps.txt'; $link->{'WorkingDirectory'} = 'C:\Documents and Settings\jim\My Docume +nts'; $link->{'ShowCmd'} = SW_SHOWNORMAL; $link->Save('C:/Documents and Settings/jim/Desktop/google_maps.lnk'); $link->Close();
    I created two links on my desktop, one the "normal" way, one with the script. They are of different sizes:

    C:\Documents and Settings\jim\Desktop>dir *.lnk Volume in drive C is mecano Volume Serial Number is 84B7-29B9 Directory of C:\Documents and Settings\jim\Desktop 07.26.05 12:35 PM 648 google_maps.lnk 07.26.05 12:24 PM 594 Shortcut to Google_maps.txt.lnk
    but seem the same on visual inspection. Both open the text file correctly without an additional prompt.

    - j

      Looking at the actual link file with a hex editor shows that the file that is created programmatically contains a complete path for one of the attributes rather than a relative directory path like the manually created one.

      It is interesting to note that the complete path supplied does not contain the drive letter, in fact windows seems to ignore that path completely since changing it does not affect the link at all. However it does require the filename at the end, if that gets changed the link will not work correctly.

      Thanks to everyone's replies - yes, I can replicate the same results. Both the Perl script generated version and the "manual" way work correctly, albeit being different sizes.
Re: Windows Shortcut Creation
by ikegami (Patriarch) on Jul 26, 2005 at 16:44 UTC

    The following works for me on ActivePerl v5.6.1 on Win2k. It appears to be the minimal solution.

    use Win32::Shortcut (); my $s = Win32::Shortcut->new(); $s->Path('C:\\TEMP'); $s->Save('C:\\TEMP.lnk'); # Test the shortcut. # Make sure C:\TEMP exists. # Make sure you have no windows displaying C:\TEMP. system('start "C:\\TEMP.lnk"');

    The above was extracted from the following longer test:

      the system command will start this within Perl, but the shortcut will not work by clicking on it.


        I beg to differ. Not only does it work for me when I click on it, I didn't use the system command to start it within Perl. I used Windows's start utility, which is (supposed to be) the same thing as clicking on it.

        Did you change anything?

Re: Windows Shortcut Creation
by Anonymous Monk on Aug 17, 2010 at 10:01 UTC
    I have had a similar problem recently under Windows Vista Business. Could not get it to work for shortcuts to a folder. It all looked OK but did not actually go to the folder when opened, just popped up the dialog asking which application to use. In the end I stopped using Wind32::Shortcut altogether. I do this instead, with appropriate definitions for $linkname and $target:
    use Win32::OLE; my $WShell = Win32::OLE->new('WScript.Shell'); my $sc = $WShell->CreateShortcut("$linkname.lnk"); if ( -d "$target") { # This looks weird but is the only way I could make it work. # A straight forward link to a folder sort of looks right but # doesn't actually work when you try to follow it. # This creates a bit of mix between a link and a url. # It is a .lnk file but it sets TargetPath to a URL like value. $sc->{'TargetPath'} = "file:///$target"; } else { $sc->{'TargetPath'} = "$target"; } $sc->Save();
Re: Windows Shortcut Creation
by Anonymous Monk on Mar 25, 2008 at 01:37 UTC
    Does anyone have a solution to this problem? The exact same thing is happening to me. Thanks, Chris
      The behaviour where you get the malfunctioning link occurs if you use a path name like
      "C:/Documents and Settings/bkb/"
      If you use
      'C:\\Documents and Settings\bkb'
      then the shortcut is created correctly.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://478252]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-12-08 20:32 GMT
Find Nodes?
    Voting Booth?

    No recent polls found