How to Create Links

Setup programs automatically can create links to your programs on the desktop, in the Start Menu or wherever you want. WinAmp allows you to create links on the desktop, in the Start menu and in the QuickLaunch menu. Wouldn’t it be cool if your program could do the same? There are Delphi components that do the job for you but there is no reason you should use VCL components. Why? Well, the whole trick is only 2 functions. Creating components for the job is just wasting space.

If you want to create links in the Start|Documents menu check out tip “How to add files to the Start|Documents menu.

The SpecialDirectory() function returns the path of the specified special directory. These are:

SpecialDirectory() Parameter Special Folder Description
CSIDL_BITBUCKET Recycle Bin The directory containing files in the recycle bin. The location of this directory is not in the registry.
CSIDL_CONTROLS Control Panel Virtual folder containing icons for the control panel applications.
CSIDL_DESKTOP Windows Desktop Virtual folder at the root of the name space.
CSIDL_DESKTOPDIRECTORY   The directory that used to store objects on the desktop (do not confuse with the desktop folder itself).
CSIDL_DRIVES My Computer Virtual folder containing everything on the local computer: storage devices, printers, and the Control Panel. May also contain mapped network drives.
CSIDL_FONTS Fonts Virtual folder containing fonts.
CSIDL_NETHOOD   he directory containing objects that appear in the network neighborhood.
CSIDL_NETWORK Network Neighborhood Virtual folder representing the top level of the network hierarchy.
CSIDL_PERSONAL   The directory that serves as a common repository for documents.
CSIDL_PRINTERS Printers Folder Virtual folder that contains installed printers.
CSIDL_PROGRAMS   The directory that contains the user’s program groups.
CSIDL_RECENT Recent Documents The directory that contains the user’s most recently used documents.
CSIDL_SENDTO Send To Folder The directory that contains Send To menu items.
CSIDL_STARTMENU Start Menu The directory containing Start menu items.
CSIDL_STARTUP Startup Menu The directory that corresponds to the user’s Startup program group.
CSIDL_TEMPLATES Templates The directory that serves as a common repository for document templates.

Step 1. Get the special directory you want (if you want to put your link in a special directory, that is:)

uses
  ComObj, ShlObj, Windows;
...
function SpecialDirectory(const iID: Integer): string;
var
  aPath  : array[0..MAX_PATH] of Char;
  pilTemp: PItemIDList;
begin
  OleCheck(SHGetSpecialFolderLocation(0, iID, pilTemp));
  SHGetPathFromIDList(pilTemp, aPath);
  CoTaskMemFree(pilTemp);
  Result := string(aPath);
end;

Example:

SpecialDirectory(CSIDL_FONTS);

Step 2. Once you get the folder path you want to create the link in all you have to do is create the link itself. Here’s a function that will do the job for you:

uses
  ActiveX, ShelObj;
...
function CreateLink(const sFile, sLinkFile, sDescription: string): Boolean;
var
  pfLink : IPersistFile;
  islLink: IShellLink;
begin
  Result := False;
  if CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IID_IShellLinkA, islLink) = NO_ERROR then
    with islLink do begin
      SetPath(PChar(sFile)));
      SetDescription(PChar(sDescription));
      if QueryInterface(IID_IPersistFile, pfLink) = NO_ERROR then begin
        pfLink.Save(StringToOLEStr(sLinkFile), True);
        Result := True;
      end;
    end;
end;

Example:

CreateLink(PChar(Application.ExeName), 'Some comment', SpecialDirectory(CSIDL_DESKTOP) + '\the program name.lnk');

This example code will create a link “the program name.lnk” commented ‘Some comment’ on the desktop.

Tags: , , , , , , , , , , , , ,

1 Comments

  1. JERONIMO
    June 26, 2008 at 03:15

    In this listings are 2 errors:
    1. CreateLink(PChar(Application.ExeName), SpecialDirectory(CSIDL_DESKTOP) + ‘\name.lnk’, ‘Coment’);
    2. IID_IPersistFile: TGUID = ( D1:$0000010B;D2:$0000;D3:$0000;D4:($C0,$00,$00,$00,$00,$00,$00,$46));

Leave a comment

OpenID Login

Standard Login