DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

CreateShellLink

Creates a shell link named LinkFileName that is a shortcut to file AssocFileName with descriprion Desc.The program executed by the shortcut has working directory WorkDir and is passed command line Args. If IconFileName is not empty then the shortcut's icon is extracted from file IconFileName at index IconIdx. Returns True on success or False on error.

function CreateShellLink(const LinkFileName, AssocFileName, Desc, WorkDir,
  Args, IconFileName: string; const IconIdx: Integer): Boolean;
var
  SL: ShlObj.IShellLink;    // shell link object
  PF: ActiveX.IPersistFile; // persistant file interface to shell link object
begin
  // Assume failure
  Result := False;
  // Ensure COM is initialised
  ActiveX.CoInitialize(nil);
  try
    // Create shell link object
    if ActiveX.Succeeded(
      ActiveX.CoCreateInstance(
        ShlObj.CLSID_ShellLink,
        nil,
        ActiveX.CLSCTX_INPROC_SERVER,
        ShlObj.IShellLink, SL
      )
    ) then
    begin
      // Store required properties of shell link
      SL.SetPath(PChar(AssocFileName));
      SL.SetDescription(PChar(Desc));
      SL.SetWorkingDirectory(PChar(WorkDir));
      SL.SetArguments(PChar(Args));
      if (IconFileName <> '') and (IconIdx >= 0) then
        SL.SetIconLocation(PChar(IconFileName), IconIdx);
      // Create persistant file interface to shell link to save link file
      PF := SL as ActiveX.IPersistFile;
      Result := ActiveX.Succeeded(
        PF.Save(PWideChar(WideString(LinkFileName)), True)
      );
    end;
  finally
    // Finalize COM
    ActiveX.CoUninitialize;
  end;
end;

View the whole database.

Go to the DelphiDabbler website.