这是一个实际的例子。
procedure MakeLink(proName, lnkName, Description: string);
var
WorkDir, DestName: string;
aReg: TRegistry;
aObj: IUnknown;
MyLink: IShellLink;
MyPFile: IPersistFile;
WFileName: WideString;
curdir: array[0..100] of Char;
begin
//操作注册表:
//键'Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders'
//下的Programs串值指定了Windows启动目录的位置.
aReg := TRegistry.create;
aReg.RootKey := HKey_Current_User;
aReg.OpenKey('Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders', False);
DestName := aReg.ReadString('Startup') + '/' + lnkName;
aReg.Closekey;
aReg.free;
aObj := CreateComObject(CLSID_ShellLink);
MyLink := aObj as IShellLink;
MyPFile := aObj as IPersistFile;
GetCurrentDirectory(sizeof(curdir), curdir);
workdir := curdir;
proname := trim(WorkDir)+'/'+proname;
with MyLink do //操作IShellLink类
begin
//设置显示窗口的类型
SetShowCmd(SW_NORMAL);
{ 对DOS程序,一般建议使用SetShowCmd(SW_SHOWMAXIMIZED); }
// 设置命令行参数
SetArguments('');
// 设置描述
SetDescription(pChar(Description));
// 设置程序名称[全路径,如果在系统Path中,可直接为程文件名]
SetPath(pChar(proName));
// 设置工作目录
SetWorkingDirectory(pChar(WorkDir));
end;
//取.LNK文件的位置
CreateDir(ExtractFilePath(DestName));
//建立快捷方式: .LNK文件
WFileName := DestName; //将一个String赋给WideString,转换过程由Delphi自动完成
MyPFile.Save(PWChar(WFileName), False);
end;
procedure TMainForm.Button1Click(Sender: TObject);
var
Code: Integer;
begin
MakeLink('yourprog.exe', 'yourprog.lnk', 'progname');
{ Get text from TEdit control }
end;
procedure TMainForm.N18Click(Sender: TObject);
begin
close;
end;
end.