<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Win32::Shortcut;
#######################################################################
#
# Win32::Shortcut - Perl Module for Shell Link Interface
# ^^^^^^^^^^^^^^^
# This module creates an object oriented interface to the Win32
# Shell Links (IShellLink interface).
#
#######################################################################

$VERSION = "0.08";

require Exporter;
require DynaLoader;

@ISA= qw( Exporter DynaLoader );
@EXPORT = qw(
    SW_SHOWMAXIMIZED
    SW_SHOWMINNOACTIVE
    SW_SHOWNORMAL
);


#######################################################################
# This AUTOLOAD is used to 'autoload' constants from the constant()
# XS function.  If a constant is not found then control is passed
# to the AUTOLOAD in AutoLoader.
#

sub AUTOLOAD {
    my($constname);
    ($constname = $AUTOLOAD) =~ s/.*:://;
    #reset $! to zero to reset any current errors.
    local $!;
    my $val = constant($constname, @_ ? $_[0] : 0);
    if ($!) {
        my(undef, $file, $line) = caller;
        die "Win32::Shortcut::$constname is not defined, used at $file line $line.";
    }
    eval "sub $AUTOLOAD { $val }";
    goto &amp;$AUTOLOAD;
}


#######################################################################
# PUBLIC METHODS
#

#========
sub new {
#========
    my($class, $file) = @_;
    my($ilink, $ifile) = _Instance();
    return unless $ilink &amp;&amp; $ifile;

    my $self = bless {
        ilink            =&gt; $ilink,
        ifile            =&gt; $ifile,
        File             =&gt; "",
        Path             =&gt; "",
        Arguments        =&gt; "",
        WorkingDirectory =&gt; "",
        Description      =&gt; "",
        ShowCmd          =&gt; 0,
        Hotkey           =&gt; 0,
        IconLocation     =&gt; "",
        IconNumber       =&gt; 0,
    };


    if ($file) {
	$self-&gt;{File} = $file;
	$self-&gt;Load($file);
    }

    return $self;
}

#=========
sub Load {
#=========
    my($self, $file) = @_;
    return undef unless ref($self);
  
    my $result = _Load($self-&gt;{'ilink'}, $self-&gt;{'ifile'}, $file);

    if ($result) {
  
        # fill the properties of $self
        $self-&gt;{'File'} = $file;
        $self-&gt;{'Path'} = _GetPath($self-&gt;{'ilink'}, $self-&gt;{'ifile'},0);
        $self-&gt;{'ShortPath'} = _GetPath($self-&gt;{'ilink'}, $self-&gt;{'ifile'},1);
        $self-&gt;{'Arguments'} = _GetArguments($self-&gt;{'ilink'}, $self-&gt;{'ifile'});
        $self-&gt;{'WorkingDirectory'} = _GetWorkingDirectory($self-&gt;{'ilink'}, $self-&gt;{'ifile'});
        $self-&gt;{'Description'} = _GetDescription($self-&gt;{'ilink'}, $self-&gt;{'ifile'});
        $self-&gt;{'ShowCmd'} = _GetShowCmd($self-&gt;{'ilink'}, $self-&gt;{'ifile'});
        $self-&gt;{'Hotkey'} = _GetHotkey($self-&gt;{'ilink'}, $self-&gt;{'ifile'});
        ($self-&gt;{'IconLocation'},
         $self-&gt;{'IconNumber'}) = _GetIconLocation($self-&gt;{'ilink'}, $self-&gt;{'ifile'});
    }
    return $result;
}


#========
sub Set {
#========
    my($self, $path, $arguments, $dir, $description, $show, $hotkey, 
       $iconlocation, $iconnumber) = @_;
    return undef unless ref($self);

    $self-&gt;{'Path'}             = $path;
    $self-&gt;{'Arguments'}        = $arguments;
    $self-&gt;{'WorkingDirectory'} = $dir;
    $self-&gt;{'Description'}      = $description;
    $self-&gt;{'ShowCmd'}          = $show;
    $self-&gt;{'Hotkey'}           = $hotkey;
    $self-&gt;{'IconLocation'}     = $iconlocation;
    $self-&gt;{'IconNumber'}       = $iconnumber;
    return 1;
}


#=========
sub Save {
#=========
    my($self, $file) = @_;
    return unless ref($self);

    $file = $self-&gt;{'File'} unless $file;
    return unless $file;

    require Win32 unless defined &amp;Win32::GetFullPathName;
    $file = Win32::GetFullPathName($file);

    _SetPath($self-&gt;{'ilink'}, $self-&gt;{'ifile'}, $self-&gt;{'Path'});
    _SetArguments($self-&gt;{'ilink'}, $self-&gt;{'ifile'}, $self-&gt;{'Arguments'});
    _SetWorkingDirectory($self-&gt;{'ilink'}, $self-&gt;{'ifile'}, $self-&gt;{'WorkingDirectory'});
    _SetDescription($self-&gt;{'ilink'}, $self-&gt;{'ifile'}, $self-&gt;{'Description'});
    _SetShowCmd($self-&gt;{'ilink'}, $self-&gt;{'ifile'}, $self-&gt;{'ShowCmd'});
    _SetHotkey($self-&gt;{'ilink'}, $self-&gt;{'ifile'}, $self-&gt;{'Hotkey'});
    _SetIconLocation($self-&gt;{'ilink'}, $self-&gt;{'ifile'},
                     $self-&gt;{'IconLocation'}, $self-&gt;{'IconNumber'});

    my $result = _Save($self-&gt;{'ilink'}, $self-&gt;{'ifile'}, $file);
    if ($result) {
	$self-&gt;{'File'} = $file unless $self-&gt;{'File'};
    }
    return $result;
}

#============
sub Resolve {
#============
    my($self, $flags) = @_;
    return undef unless ref($self);
    $flags = 1 unless defined($flags);
    my $result = _Resolve($self-&gt;{'ilink'}, $self-&gt;{'ifile'}, $flags);
    return $result;
}


#==========
sub Close {
#==========
    my($self) = @_;
    return undef unless ref($self);

    my $result = _Release($self-&gt;{'ilink'}, $self-&gt;{'ifile'});
    $self-&gt;{'released'} = 1;
    return $result;
}

#=========
sub Path {
#=========
    my($self, $value) = @_;
    return undef unless ref($self);

    if(not defined($value)) {
        return $self-&gt;{'Path'};
    } else {
        $self-&gt;{'Path'} = $value;
    }
    return $self-&gt;{'Path'};
}

#==============
sub ShortPath {
#==============
    my($self) = @_;
    return undef unless ref($self);
    return $self-&gt;{'ShortPath'};
}

#==============
sub Arguments {
#==============
    my($self, $value) = @_;
    return undef unless ref($self);

    if(not defined($value)) {
        return $self-&gt;{'Arguments'};
    } else {
        $self-&gt;{'Arguments'} = $value;
    }
    return $self-&gt;{'Arguments'};
}

#=====================
sub WorkingDirectory {
#=====================
    my($self, $value) = @_;
    return undef unless ref($self);

    if(not defined($value)) {
        return $self-&gt;{'WorkingDirectory'};
    } else {
        $self-&gt;{'WorkingDirectory'} = $value;
    }
    return $self-&gt;{'WorkingDirectory'};
}


#================
sub Description {
#================
    my($self, $value) = @_;
    return undef unless ref($self);

    if(not defined($value)) {
        return $self-&gt;{'Description'};
    } else {
        $self-&gt;{'Description'} = $value;
    }
    return $self-&gt;{'Description'};
}

#============
sub ShowCmd {
#============
    my($self, $value) = @_;
    return undef unless ref($self);

    if(not defined($value)) {
        return $self-&gt;{'ShowCmd'};
    } else {
        $self-&gt;{'ShowCmd'} = $value;
    }
    return $self-&gt;{'ShowCmd'};
}

#===========
sub Hotkey {
#===========
    my($self, $value) = @_;
    return undef unless ref($self);

    if(not defined($value)) {
        return $self-&gt;{'Hotkey'};
    } else {
        $self-&gt;{'Hotkey'} = $value;
    }
    return $self-&gt;{'Hotkey'};
}

#=================
sub IconLocation {
#=================
    my($self, $value) = @_;
    return undef unless ref($self);

    if(not defined($value)) {
        return $self-&gt;{'IconLocation'};
    } else {
        $self-&gt;{'IconLocation'} = $value;
    }
    return $self-&gt;{'IconLocation'};
}

#===============
sub IconNumber {
#===============
    my($self, $value) = @_;
    return undef unless ref($self);

    if(not defined($value)) {
        return $self-&gt;{'IconNumber'};
    } else {
        $self-&gt;{'IconNumber'} = $value;
    }
    return $self-&gt;{'IconNumber'};
}

#============
sub Version {
#============
    # [dada] to get rid of the "used only once" warning...
    return $VERSION;
}


#######################################################################
# PRIVATE METHODS
#

#============
sub DESTROY {
#============
    my($self) = @_;

    if (not $self-&gt;{'released'}) {
        _Release($self-&gt;{'ilink'}, $self-&gt;{'ifile'});
	$self-&gt;{'released'} = 1;
    }
}

bootstrap Win32::Shortcut;

1;

__END__

=head1 NAME

Win32::Shortcut - Perl Module to deal with Windows Shortcuts

=head1 SYNOPSIS

This module implements the Win32 IShellLink Interface to allow
management of shortcut files from Perl.

  use Win32::Shortcut;

  $LINK = Win32::Shortcut-&gt;new();
  $LINK-&gt;{'Path'} = "C:\\Directory\\Target.exe";
  $LINK-&gt;{'Description'} = "Target executable";
  $LINK-&gt;Save("Target.lnk");
  $LINK-&gt;Close();

=head1 REFERENCE

=head2 General Usage

To use this module, first add the following line at the beginning of
your script:

  use Win32::Shortcut;

Then, use this command to create a shortcut object:

  $LINK = Win32::Shortcut-&gt;new();

This function will create a C&lt;$LINK&gt; object on which you can apply the
Methods and Properties explained later.

The object is not yet a shortcut file; it is just the definition of a
shortcut. Basically, you can do 2 things:

=over

=item 1. Load a shortcut file into the object.

=item 2. Save the object into a shortcut file.

=back

For the rest, the object can be accessed as it were a normal
associative array reference. It has the following keys (here referred
as properties):

  $LINK-&gt;{'File'}
  $LINK-&gt;{'Path'}               $LINK-&gt;Path()
  $LINK-&gt;{'ShortPath'}
  $LINK-&gt;{'WorkingDirectory'}   $LINK-&gt;WorkingDirectory()
  $LINK-&gt;{'Arguments'}          $LINK-&gt;Arguments()
  $LINK-&gt;{'Description'}        $LINK-&gt;Description()
  $LINK-&gt;{'ShowCmd'}            $LINK-&gt;ShowCmd()
  $LINK-&gt;{'Hotkey'}             $LINK-&gt;Hotkey()
  $LINK-&gt;{'IconLocation'}       $LINK-&gt;IconLocation()
  $LINK-&gt;{'IconNumber'}         $LINK-&gt;IconNumber()

Thus, assuming you have a shortcut file named C&lt;test.lnk&gt; in your
current directory, this simple script will tell you where this shortcut
points to:

  use Win32::Shortcut;
  $LINK = Win32::Shortcut-&gt;new();
  $LINK-&gt;Load("test.lnk");
  print "Shortcut to: $LINK-&gt;{'Path'} $LINK-&gt;{'Arguments'} \n";
  $LINK-&gt;Close();

But you can also modify its values:

  use Win32::Shortcut;
  $LINK = Win32::Shortcut-&gt;new();
  $LINK-&gt;Load("test.lnk");
  $LINK-&gt;{'Path'} =~ s/C:/D:/i;   # move the target from C: to D:
  $LINK-&gt;{'ShowCmd'} = SW_NORMAL; # runs in a normal window

and then you can save your changes to the shortcut file with this
command:

  $LINK-&gt;Save();
  $LINK-&gt;Close();

or you can save it with another name, creating a new shortcut file:

  $LINK-&gt;Save("test2.lnk");
  $LINK-&gt;Close();

Finally, you can create a completely new shortcut:

  $LINK = Win32::Shortcut-&gt;new();
  $LINK-&gt;{'Path'} = "C:\\PERL5\\BIN\\PERL.EXE";
  $LINK-&gt;{'Arguments'} = "-v";
  $LINK-&gt;{'WorkingDirectory'} = "C:\PERL5\\BIN";
  $LINK-&gt;{'Description'} = "Prints out the version of Perl";
  $LINK-&gt;{'ShowCmd'} = SW_SHOWMAXIMIZED;
  $LINK-&gt;Save("Perl Version Info.lnk");
  $LINK-&gt;Close();

Note also that in the examples above the two lines:

  $LINK = Win32::Shortcut-&gt;new();
  $LINK-&gt;Load("test.lnk");

can be collapsed to:

  $LINK = Win32::Shortcut-&gt;new("test.lnk");


=head2 Methods

=over

=item B&lt;Close&gt;

Closes a shortcut object. Note that it is not "strictly" required to
close the objects you created, since the Win32::Shortcut objects are
automatically closed when the program ends (or when you somehow destroy
such an object).

Note also that a shortcut is not automatically saved when it is closed,
even if you modified it. You have to call Save in order to apply
modifications to a shortcut file.

Example:

  $LINK-&gt;Close();

=item B&lt;Load&gt; I&lt;file&gt;

Loads the content of the shortcut file named I&lt;file&gt; in a shortcut
object and fill the properties of the object with its values. Will
return B&lt;undef&gt; on errors, or a true value if everything was
successful.

Example:

  $LINK-&gt;Load("test.lnk") or print "test.lnk not found!";

  print join("\n", $LINK-&gt;Path,
                   $LINK-&gt;ShortPath,
                   $LINK-&gt;Arguments,
                   $LINK-&gt;WorkingDirectory,
                   $LINK-&gt;Description,
                   $LINK-&gt;ShowCmd,
                   $LINK-&gt;Hotkey,
                   $LINK-&gt;IconLocation,
                   $LINK-&gt;IconNumber);
  }

=item B&lt;new Win32::Shortcut&gt; I&lt;[file]&gt;

Creates a new shortcut object. If a filename is passed in I&lt;file&gt;,
automatically Loads this file also. Returns the object created or
B&lt;undef&gt; on errors.

Example:

  $LINK = Win32::Shortcut-&gt;new();

  $RegEdit = Win32::Shortcut-&gt;new("Registry Editor.lnk");
  die "File not found" if not $RegEdit;

=item B&lt;Resolve&gt; I&lt;[flag]&gt;

Attempts to automatically resolve a shortcut and returns the resolved
path, or B&lt;undef&gt; on errors; in case no resolution was made, the path
is returned unchanged. Note that the path is automatically updated in
the Path property of the shortcut.

By default this method acts quietly, but if you pass a value of 0
(zero) in the I&lt;flag&gt; parameter, it will eventually post a dialog box
prompting the user for more information.

Example:

  # if the target doesn't exist...
  if(! -f $LINK-&gt;Path) {
    # save the actual target for comparison
    $oldpath = $LINK-&gt;Path;

    # try to resolve it (with dialog box)
    $newpath = $LINK-&gt;Resolve(0);

    die "Not resolved..." if $newpath == $oldpath;
  }

=item B&lt;Save&gt; I&lt;[file]&gt;

Saves the content of the shortcut object into the file named I&lt;file&gt;.
If I&lt;file&gt; is omitted, it is taken from the File property of the object
(which, if not changed, is the name of the last Loaded file).

If no file was loaded and the File property doesn't contain a valid
filename, the method will return B&lt;undef&gt;, which will also be returned
on errors. A true value will be returned if everything was successful.

Example:

 $LINK-&gt;Save();
 $LINK-&gt;Save("Copy of " . $LINK-&gt;{'File'});

=item B&lt;Set&gt; I&lt;path, arguments, workingdirectory, description, showcmd,
hotkey, iconlocation, iconnumber&gt;

Sets all the properties of the shortcut object with a single command.
This method is supplied for convenience only, you can also set those
values changing the values of the properties.

Example:

  $LINK-&gt;Set("C:\\PERL5\\BIN\\PERL.EXE",
             "-v",
             "C:\\PERL5\\BIN",
             "Prints out the version of Perl",
             SW_SHOWMAXIMIZED,
             hex('0x0337'),
             "C:\\WINDOWS\\SYSTEM\\COOL.DLL",
             1);

  # it is the same of...
  $LINK-&gt;Path("C:\\PERL5\\BIN\\PERL.EXE");
  $LINK-&gt;Arguments("-v");
  $LINK-&gt;WorkingDirectory("C:\\PERL5\\BIN");
  $LINK-&gt;Description("Prints out the version of Perl");
  $LINK-&gt;ShowCmd(SW_SHOWMAXIMIZED);
  $LINK-&gt;Hotkey(hex('0x0337'));
  $LINK-&gt;IconLocation("C:\\WINDOWS\\SYSTEM\\COOL.DLL");
  $LINK-&gt;IconNumber(1);

=back

=head2 Properties

The properties of a shortcut object can be accessed as:

  $OBJECT-&gt;{'property'}

Eg., assuming that you have created a shortcut object with:

  $LINK=new Win32::Shortcut();

you can for example see its description with:

  print $LINK-&gt;{'Description'};

You can of course also set it:

  $LINK-&gt;{'Description'}="This is a description";

From version 0.02, those properties have also a corresponding method
(subroutine), so you can write the 2 lines above using this syntax too:

  print $LINK-&gt;Description;
  $LINK-&gt;Description("This is a description");

The properties of a shortcut reflect the content of the Shortcut
Properties Dialog Box, which can be obtained by clicking the third
mouse button on a shortcut file in the Windows 95 (or NT 4.0) Explorer
and choosing "Properties" (well, I hope you already knew :).

The fields corresponding to the single properties are marked in B&lt;bold&gt;
in the following list.

=over

=item B&lt;Arguments&gt;

The arguments associated with the shell link object. They will be
passed to the targeted program (see Path) when it gets executed. In
fact, joined with Path, this parameter forms the "B&lt;Target&gt;" field of a
Shortcut Properties Dialog Box.

=item B&lt;Description&gt;

An optional description given to the shortcut. Seems to be missing in
the Shortcut Properties Dialog Box (not yet implemented?).

=item B&lt;File&gt;

The filename of the shortcut file opened with Load, and/or the filename
under which the shortcut will be saved with Save (if the I&lt;file&gt;
argument is not specified).

=item B&lt;Hotkey&gt;

The hotkey associated to the shortcut, in the form of a 2-byte number
of which the first byte identifies the modifiers (Ctrl, Alt, Shift...
but I didn't find out how it works) and the second is the ASCII code of
the character key. Correspond to the "B&lt;Shortcut key&gt;" field of a
Shortcut Properties Dialog Box.

=item B&lt;IconLocation&gt;

The file that contain the icon for the shortcut. Seems actually to
always return nothing (YMMV, I hope...).

=item B&lt;IconNumber&gt;

The number of the icon for the shortcut in the file pointed by
IconLocation, in case more that one icon is contained in that file (I
suppose this, documentation isn't so clear at this point).

=item B&lt;Path&gt;

The target of the shortcut. This is (joined with Arguments) the content
of the "B&lt;Target&gt;" field in a Shortcut Properties Dialog Box.

=item B&lt;ShortPath&gt;

Same as Path, but expressed in a DOS-readable format (8.3 characters
filenames). It is available as read-only (well, you can change it, but
it has no effect on the shortcut; change Path instead) once you Load a
shortcut file.

=item B&lt;ShowCmd&gt;

The condition of the window in which the program will be executed (can
be Normal, Minimized or Maximized). Correspond to the "B&lt;Run&gt;" field of
a Shortcut Properties Dialog Box.

Allowed values are:

B&lt;Value&gt;    B&lt;Meaning&gt;       B&lt;Constant&gt;

   1     Normal Window SW_SHOWNORMAL
   3     Maximized     SW_SHOWMAXIMIZED
   7     Minimized     SW_SHOWMINNOACTIVE

Any other value (theoretically should) result in a Normal Window
display.

=item B&lt;WorkingDirectory&gt;

The directory in which the targeted program will be executed.
Correspond to the "B&lt;Start in&gt;" field of a Shortcut Properties Dialog
Box.

=back

=head2 Constants

The following constants are exported in the main namespace of your
script using Win32::Shortcut:

=over

=item * SW_SHOWNORMAL

=item * SW_SHOWMAXIMIZED

=item * SW_SHOWMINNOACTIVE

=back

Those constants are the allowed values for the ShowCmd property.



=head1 VERSION HISTORY

B&lt;0.03 (07 Apr 1997)&gt;

=over

=item * The PLL file now comes in 2 versions, one for Perl version
5.001 (build 110) and one for Perl version 5.003 (build 300 and higher,
EXCEPT 304).

=item * Added an installation program which will automatically copy the
right files in the right place.

=back

B&lt;0.02 (21 Jan 1997)&gt;

=over

=item * Added methods matching properties to reduce typing overhead
(eg. Alt-123 and 125...).

=back

B&lt;0.01 (15 Jan 1997)&gt;

=over

=item *

First public release.

=item *

Added "Set" and "Resolve" and the properties "Hotkey", "IconLocation"
and "IconNumber".

=back

B&lt;0.01a (10 Jan 1997)&gt;

=over

=item *

First implementation of the IShellLink interface (wow, it works!); can
"Load", "Save", and modify properties of shortcut files.

=back

=head1 AUTHOR

Aldo Calpini L&lt;dada@perl.it&gt;

Distributed under the terms of Larry Wall's Artistic License.

=head1 CREDITS

Thanks to: Jesse Dougherty, Dave Roth, ActiveWare, and the
Perl-Win32-Users community.

=head1 DISCLAIMER

I&lt;This program is FREE; you can redistribute, modify, disassemble, or
even reverse engineer this software at your will. Keep in mind,
however, that NOTHING IS GUARANTEED to work and everything you do is AT
YOUR OWN RISK - I will not take responsibility for any damage, loss of
money and/or health that may arise from the use of this program!&gt;

=cut
</pre></body></html>