Mega Code Archive

 
Categories / Delphi / System
 

How to create a Shell Link

Title: How to create a Shell Link Question: How to create the binary LNK file from delphi. Answer: More and more windows api functions are provides as COM interfaces. The IShellLink is one of them. It can be used to create a link on the windows desktop. uses activeX,comobj,shlobj; const IID_IPersistFile: TGUID = (D1:$0000010B;D2:$0000;D3:$0000;D4:($C0,$00,$00,$00,$00,$00,$00,$46)); function CreateLink(aPathObj,aPathLink,aDesc: string): boolean; var sLink: IShellLink; PersFile: IPersistFile; begin Result := false; if SUCCEEDED(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IID_IShellLinkA, sLink)) then begin sLink.SetPath(PChar(aPathObj)); sLink.SetDescription(PChar(aDesc)); if SUCCEEDED(sLink.QueryInterface(IID_IPersistFile,PersFile)) then begin PersFile.Save(StringToOLEStr(aPathLink),TRUE); Result := true; end; end; end; procedure TForm1.Button1Click(Sender: TObject); begin if not CreateLink('command.com', 'c:\temp\mylink.lnk','description...') then begin // errorhandling end; end;