PhysiologyWeb Logo  Search
Share on Facebook Share on Twitter Share on LinkedIn Share on Pinterest Email Copy URL
PhysiologyWeb Loading...

Delphi Code - Create Shortcut
Here is a simple Delphi function that saves a shortcut link in a specified folder. For additional Delphi code for the Windows operating system, see Useful Delphi Code for the Windows Operating System.
function CreateShortcut(DestinationFolder, TargetFile, LinkName, LinkDescription: String): Boolean;
var
IObject: IUnknown; //uses System unit
ISLink: IShellLink; //uses ShlObj unit
IPFile: IPersistFile; //uses ActiveX unit
LinkPath: String;
begin
CreateShortcut := False;

LinkPath := '';

try
IObject := CreateComObject(CLSID_ShellLink); //uses ComObj unit
ISLink := IObject as IShellLink;
IPFile := IObject as IPersistFile;

with ISLink do
begin
SetPath(PChar(TargetFile)); //uses ShlObj unit
SetWorkingDirectory(PChar(ExtractFilePath(TargetFile))); //uses ShlObj unit
If LinkDescription <> '' then SetDescription(PChar(LinkDescription)); //uses ShlObj unit
end;

LinkPath := DestinationFolder + '\' + LinkName;
LinkPath := ChangeFileExt(LinkPath, '.lnk');

IPFile.Save(PWChar(LinkPath), False); //uses ActiveX unit
IPFile.SaveCompleted(PWChar(LinkPath)); //uses ActiveX unit
except
LinkPath := '';
end;

If FileExists(LinkPath) = True then CreateShortcut := True else CreateShortcut := False;
end;
The procedure below uses the function shown above to create the shortcut link.
procedure TForm1.Button1(Sender: TObject);
begin
CreateShortcut('C:\PhysiologyWeb', 'C:\0\test.xlsx', 'test.xlsx', 'Excel file: test.xlsx');
end;
Often, it is useful to save the shortcut on the Windows desktop or in some other special folder. To obtain the location of Windows special folders, please see Get the location for Windows special folders (e.g., desktop, start menu, program files, fonts, AppData, etc.).






Posted: Monday, September 4, 2023