The Unofficial Newsletter of Delphi Users - by Robert Vivrette




Taking A Shortcut...

by Robert Vivrette - RobertV@csi.com & Peter Lennard Larsen - s01pll@msektor.auc.dk

Do you need to have your Delphi app create shortcuts on the Desktop? Perhaps it needs to install another application and you want control over where it is installed and the icons that are created for it. Well, you need look no further.

Prior to Windows 95, the appropriate way to perform these techniques was through a DDE conversation with the Windows Program Manager. Times change and so does our operating system. When Win95 was introduced they changed the whole concept of creating program groups, and adding icons to the operating system. We know them as ShortCuts, but there is a lot going on behind the scenes to support them. Shortcuts are created and managed through an interface called IShellLink and there have been a few articles on their structure and use that can be found on the Microsoft Developers Network. Some information on this interface can also be found in the Delphi Help files under the keyword ISHELLLINK.

Peter had originally sent me a great little unit that defined an object that did all this for you. However, it was only compatible with Delphi 2 and I had a few problems getting it to work under Delphi 3. It did spark what few creative juices I have however, so I decided to create my own version of his work in Delphi 3. It performs essentially the same operation, but is a procedure in a unit (rather than an object). It also is slightly extended in the fact that it allows you to define the location that you want the shortcut to appear as a parameter of the CreateLink call.

If you are interested in Peter's Delphi 2 code of this technique, you can click here. If you are interested in the Delphi 3 code, I have listed a little of it below and have included a demo of its use which you can download by clicking here.

Below is the Delphi 3 unit that implements the shortcut creation mechanism. This technique can of course easily be extended to provide support for some of the additional features of the IShellLink interface. As a supplement to this capability, Peter put together a great unit called WinInfo which gives access to some of the more common destinations of Shortcuts (like the Desktop, Start Menu, etc.) You can see this article, by clicking here.

Hope you enjoy this. If you have questions, feel free to contact myself or Peter.
 

unit Shortcut;

interface

uses ShlObj, ActiveX, ComObj, Windows, WinInfo;

// TARGET is the file that the shortcut will point to.

// ARGS is the command-line arguments you wish to pass to the Target app.

// WORKDIR is the directory that will be current when the link is launched.

// SHORTCUTNAME is the name of the shortcut link file.

// LOC is the location the file should go. Uses the WinInfo unit to get the location
//   of special system locations. Set to slNone if ShortCutName fully defines the
//   location of the shortcut.

type
  TSpecialLocations = (slNone,slDesktop,slFavorites,slFonts,slNetHood,slPersonal,
                       slPrograms,slRecent,slSendTo,slStartMenu,slStartup,slTemplates);

procedure CreateLink(Target,Args,WorkDir,ShortCutName: String; Loc: TSpecialLocations);

implementation

procedure CreateLink(Target,Args,WorkDir,ShortCutName: String; Loc: TSpecialLocations);
var
  IObj    : IUnknown;
  Link    : IShellLink;
  IPFile  : IPersistFile;
  TargetW : WideString;
begin
  IObj := CreateComObject(CLSID_ShellLink);
  Link := IObj as IShellLink;
  IPFile  := IObj as IPersistFile;
  with Link do
    begin
      SetPath(PChar(Target));
      SetArguments(PChar(Args));
      SetWorkingDirectory(PChar(WorkDir));
    end;
  case Loc of
    slDesktop   : TargetW := WindowsInfo.DesktopDir+'\'+ShortCutName;
    slFavorites : TargetW := WindowsInfo.FavoritesDir+'\'+ShortCutName;
    slFonts     : TargetW := WindowsInfo.FontsDir+'\'+ShortCutName;
    slNetHood   : TargetW := WindowsInfo.NetHoodDir+'\'+ShortCutName;
    slPersonal  : TargetW := WindowsInfo.PersonalDir+'\'+ShortCutName;
    slPrograms  : TargetW := WindowsInfo.ProgramsDir+'\'+ShortCutName;
    slRecent    : TargetW := WindowsInfo.RecentDir+'\'+ShortCutName;
    slSendTo    : TargetW := WindowsInfo.SendToDir+'\'+ShortCutName;
    slStartMenu : TargetW := WindowsInfo.StartMenuDir+'\'+ShortCutName;
    slStartup   : TargetW := WindowsInfo.StartupDir+'\'+ShortCutName;
    slTemplates : TargetW := WindowsInfo.TemplatesDir+'\'+ShortCutName;
  else            TargetW := ShortCutName;
  end;
  IPFile.Save(PWChar(TargetW),False);
end;

end.