yoy.be "Why-o-Why"

Create a shortcut from code.

2006-06-08 10:04  i650  delphi  [permalink]

I've worked with the Shell COM interfaces before, and always thought it shouldn't be that hard to create a ShortCut from code, but I never got round to actually trying to do it... Until now, this is what it could look like:

uses ComObj, ActiveX, ShlObj;
 
type
  IPersistFile = interface(IPersist)
    ['{0000010B-0000-0000-C000-000000000046}']
    function IsDirty: HResult; stdcall;
    function Load(pszFileName: PWideChar; dwMode: LongWord): HResult; stdcall;
    function Save(pszFileName: PWideChar; fRemember: Integer): HResult; stdcall;
    function SaveCompleted(pszFileName: PWideChar): HResult; stdcall;
    function GetCurFile(out ppszFileName: PWideChar): HResult; stdcall;
  end;
 
var
  ln:IShellLink;
begin
  ln:=CreateComObject(CLSID_ShellLink) as IShellLink;
  OleCheck(ln.SetDescription(PChar('test')));
  OleCheck(ln.SetPath(PChar('C:\somefolder\somefile.exe')));
  (ln as IPersistFile).Save('test.lnk',0);
end;

Oddly enough, I got the IPersistFile definition from MSDASC_TLB (a TypeLibrary to create/edit database/datasource connections with) and didn't find a default Delphi6 unit that defines it.

IShellLink also provides access to other properties of the ShortCut:


twitter reddit linkedin facebook