Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; Procedure submit (Sender: tobject ); end; var form1: tform1; implementation {$ R *. DFM} uses shlobj, ActiveX, comobj; {unit used by the function} {Function Description:} {the first parameter is required to create a shortcut file; others are optional parameter} {the second parameter is the shortcut name. By default, the file name of parameter 1 is used} {the third parameter is the specified destination folder, and the default destination is the desktop; if there is a fourth parameter, this parameter will be ignored} {the fourth parameter is to specify the destination folder as a constant; this series of constants are defined in the shlobj unit, csidl _ headers} function createshortcut (exe: string; lnk: String = ''; Dir: String =''; ID: integer =-1): Boolean; var iobj: iunknown; ilnk: ishelllink; ipfile: ipersistfile; pidl: pitemidlist; infolder: array [0 .. max_path] of char; linkfilename: widestring; begin result: = false; if not fileexists (exe) Then exit; If lnk = ''then lnk: = changefileext (extractfilename (exe ), ''); iobj: = createcomobject (clsid_shelllink); ilnk: = iobj as ishelllink; ilnk. setpath (pchar (exe); ilnk. setworkingdirectory (pchar (extractfilepath (exe); If (DIR = '') and (ID =-1) Then ID: = csidl_desktop; if Id>-1 then begin shgetspecialfolderlocation (0, ID, pidl); shgetpathfromidlist (pidl, infolder); linkfilename: = format ('% s \ % S. lnk ', [infolder, lnk]); End else begin dir: = excludetrailingpathdelimiter (DIR); if not directoryexists (DIR) Then exit; linkfilename: = format ('% s \ % S. lnk ', [Dir, lnk]); end; ipfile: = iobj as ipersistfile; If ipfile. save (pwidechar (linkfilename), false) = 0 then result: = true; end; {createshortcut function end} {Test 1: set the currentProgram Create a shortcut on the desktop} procedure tform1.button1click (Sender: tobject); begin createshortcut (application. exename); end; {Test 2: Create a shortcut on the desktop and specify the shortcut name} procedure tform1.button2click (Sender: tobject); begin createshortcut (application. exename, 'newlinkname'); end; {Test 3: Create a shortcut under c: \} procedure tform1.button3click (Sender: tobject); begin createshortcut (application. exename, '', 'c: \ '); end; {Test 3: Create a shortcut in the program folder of the Start Menu} procedure tform1.button4click (Sender: tobject ); begin createshortcut (application. exename, '','', csidl_programs); end.
For the third, four parameters can be referred to: http://www.cnblogs.com/del/archive/2008/09/23/1297323.html