Create a windows shortcut with Lazarus on Desktop
In this example the following features are covered.
- How to create a shortcut from a lazarus application.
- How to get special folders such us Desktop, Program files, etc.
The following units are used: ShlObj, ActiveX, ComObj, windows
- Add the needed units.
- Create a Form1 plus one button.
- Add the following code.
procedure TForm1.Button1Click(Sender: TObject); var IObject : IUnknown; ISLink : IShellLink; IPFile : IPersistFile; PIDL : PItemIDList; InFolder : array[0..MAX_PATH] of Char; TargetName : String; LinkName : WideString; AppDataPath: Array[0..MaxPathLen] of Char; //Allocate memory begin TargetName := 'c:\windows\notepad.exe'; IObject := CreateComObject(CLSID_ShellLink) ; ISLink := IObject as IShellLink; IPFile := IObject as IPersistFile; with ISLink do begin SetPath(pChar(TargetName)) ; SetWorkingDirectory(pChar(ExtractFilePath(TargetName))) ; //to store the path AppDataPath:=''; SHGetSpecialFolderPath(0,AppDataPath,CSIDL_DESKTOPDIRECTORY,false); LinkName := AppDataPath + '\Delphi Created Link.lnk'; //to create the shortcut IPFile.Save(PWChar(LinkName), false) ; end end;
* the unit unit shlobj; contains all the constants for the Folders. The function SHGetSpecialFolderPath helps us retrieve the desired special folder. Use Control+Left-click with lazarus on CSIDL_DESKTOPDIRECTORY to see all the special folders.