Filelink: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Filelink
| Old | New | Line | |
|---|---|---|---|
| 1 | + | This tutorial is about Windows programming. This tutorial shows how to create a file link (shortcut) under Windows. | |
| 2 | + | ||
| 3 | + | The following units are required: | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | uses | |
| 7 | + | Registry, ActiveX, ComObj, ShlObj, SysUtils; | |
| 8 | + | ``` | |
| 9 | + | ||
| 10 | + | An enumeration is used to tell the function where the link should be saved. | |
| 11 | + | ||
| 12 | + | Depending on where the function is called, this list must be declared either in the [[Interface|interface]] or in the [[Implementation|implementation]] section of the [[Unit|unit]]. | |
| 13 | + | ||
| 14 | + | Definition of the list: | |
| 15 | + | ||
| 16 | + | ``` pascal | |
| 17 | + | Type | |
| 18 | + | ShortcutType = (_DESKTOP, _QUICKLAUNCH, _SENDTO, _STARTMENU, _OTHERFOLDER); | |
| 19 | + | ``` | |
| 20 | + | ||
| 21 | + | The function for creating a link (file links) looks like this: | |
| 22 | + | ||
| 23 | + | ``` pascal | |
| 24 | + | // This function works with the data of the current user | |
| 25 | + | function funErstelleLink( // Function name | |
| 26 | + | const strSourceFile : string; // Path and name of the original file | |
| 27 | + | const enuLocation : ShortcutType; // Where the shortcut should be saved | |
| 28 | + | const subdirectory : string = ''; // Optional: Alternative path to | |
| 29 | + | // save the link | |
| 30 | + | const strWorking directory : string = ''; // Optional: path or directory in the | |
| 31 | + | // the program should work | |
| 32 | + | const strParameter : string = ''; // Optional: transfer parameters to the program | |
| 33 | + | const strDescription : string = ''; // Optional: Description of the shortcut | |
| 34 | + | const strIconFile : string = ''; // Optional: file that contains the icon | |
| 35 | + | const intIconNummer : integer = 0 // Optional: The number of the icon in | |
| 36 | + | // the icon file to use for the shortcut icon | |
| 37 | + | // (the number of icons starts at 1) | |
| 38 | + | ) : string; | |
| 39 | + | ||
| 40 | + | const | |
| 41 | + | SHELL_FOLDERS_ROOT = 'Software\MicroSoft\Windows\CurrentVersion\Explorer'; | |
| 42 | + | QUICK_LAUNCH_ROOT = 'Software\MicroSoft\Windows\CurrentVersion\GrpConv'; | |
| 43 | + | ||
| 44 | + | var | |
| 45 | + | MyObject : IUnknown; | |
| 46 | + | MySLink : IShellLink; | |
| 47 | + | MyPFile : IPersistFile; | |
| 48 | + | Reg : TRegIniFile; | |
| 49 | + | strDirectory : string = ''; | |
| 50 | + | strLinkName : string = ''; | |
| 51 | + | wstrfilename : WideString = ''; | |
| 52 | + | ||
| 53 | + | begin | |
| 54 | + | ||
| 55 | + | Result := ''; | |
| 56 | + | ||
| 57 | + | // Creates the objects | |
| 58 | + | MyObject := CreateComObject(CLSID_ShellLink); | |
| 59 | + | MySLink := MyObject as IShellLink; | |
| 60 | + | MyPFile := MyObject as IPersistFile; | |
| 61 | + | ||
| 62 | + | // Creates the name of the link | |
| 63 | + | strLinkName := ChangeFileExt(strSourceFile , '.lnk'); | |
| 64 | + | strLinkName := ExtractFileName(strLinkName); | |
| 65 | + | ||
| 66 | + | // Reads the location from the registry | |
| 67 | + | if enuLocation = _QUICKLAUNCH then | |
| 68 | + | begin | |
| 69 | + | Reg := TRegIniFile.Create(QUICK_LAUNCH_ROOT); | |
| 70 | + | try | |
| 71 | + | strDirectory := Reg.ReadString('MapGroups', 'Quick Launch', ''); | |
| 72 | + | finally | |
| 73 | + | Reg.Free; | |
| 74 | + | end; | |
| 75 | + | end | |
| 76 | + | else | |
| 77 | + | begin | |
| 78 | + | Reg := TRegIniFile.Create(SHELL_FOLDERS_ROOT); | |
| 79 | + | try | |
| 80 | + | case enuLocation of | |
| 81 | + | _OTHERFOLDER : strDirectory := strDirectory; | |
| 82 | + | _DESKTOP : strDirectory := Reg.ReadString('Shell Folders', 'Desktop', ''); | |
| 83 | + | _STARTMENU : strDirectory := Reg.ReadString('Shell Folders', 'Start Menu', ''); | |
| 84 | + | _SENDTO : strDirectory := Reg.ReadString('Shell Folders', 'SendTo', ''); | |
| 85 | + | end ; | |
| 86 | + | finally | |
| 87 | + | Reg.Free; | |
| 88 | + | end; | |
| 89 | + | end; | |
| 90 | + | ||
| 91 | + | // If something went wrong, the program ends here | |
| 92 | + | // a link is not created | |
| 93 | + | if strDirectory = '' then | |
| 94 | + | exit; | |
| 95 | + | ||
| 96 | + | // Passes the appropriate parameters to the objects | |
| 97 | + | MySLink.SetPath(PChar(strSourceFile)) ; | |
| 98 | + | MySLink.SetArguments(PChar(strParameter)); | |
| 99 | + | MySLink.SetDescription(PChar(strDescription)); | |
| 100 | + | ||
| 101 | + | // If the information about the icon is plausible, it will be adopted | |
| 102 | + | // otherwise the default settings are used | |
| 103 | + | if (strIconFile <> '') and (intIconNumber > 0) then | |
| 104 | + | MySLink.SetIconLocation(PChar(strIconFile), intIconNumber); | |
| 105 | + | ||
| 106 | + | // Defines in which directory the link is saved | |
| 107 | + | if (strSubdirectory <> '') and (enuLocation <> _OTHERFOLDER) then | |
| 108 | + | wstrFilename := strDirectory + '\' + strSubDirectory + '\' + strLinkName | |
| 109 | + | else | |
| 110 | + | wstrFilename := strDirectory + '\' + strLinkName; | |
| 111 | + | ||
| 112 | + | // Specifies the directory in which the program is executed | |
| 113 | + | if strWorkingDirectory = '' then | |
| 114 | + | MySLink.SetWorkingDirectory(PChar(ExtractFilePath(strSourceFile))) | |
| 115 | + | else | |
| 116 | + | MySLink.SetWorkingDirectory(PChar(strWorkingDirectory)); | |
| 117 | + | ||
| 118 | + | // Creates the link | |
| 119 | + | MyPFile.Save(PWideChar(wstrFilename), False); | |
| 120 | + | ||
| 121 | + | // Returns the path of the link as a return value | |
| 122 | + | Result := string(wstrfilename); | |
| 123 | + | ||
| 124 | + | end ; | |
| 125 | + | ``` | |
| 126 | + | ||
| 127 | + | The function can be called as follows. | |
| 128 | + | ||
| 129 | + | ``` pascal | |
| 130 | + | funErstelleLink ( | |
| 131 | + | 'E:\Test\Test.Exe', // Path and name of the original file | |
| 132 | + | _DESKTOP, // The link should be saved here | |
| 133 | + | '', // Optional: An alternative path to save the link | |
| 134 | + | '', // Optional: The working directory of the program | |
| 135 | + | '', // Optional: transfer parameters to the program | |
| 136 | + | 'This is a test', // Optional: A description of the shortcut | |
| 137 | + | 'E:\Test\Test.Exe', // Optional: A file that contains the alternative icon | |
| 138 | + | 2 // Optional: The icon that should be displayed from the icon file | |
| 139 | + | // (the counting of the icons starts at 1) | |
| 140 | + | ) ; | |
| 141 | + | ``` | |
| 142 | + | ||
| 143 | + | --- | |
| 144 | + | *From the Free Pascal wiki, page "Filelink", as it stood on 2020-08-28 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
