2006-06-08 10:04
i650
[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:
OleCheck(ln.SetWorkingDirectory(PChar('C:\somefolder'));OleCheck(ln.SetArguments(PChar('...'));OleCheck(ln.SetIconLocation(PChar('C:\somefolder\somefile.exe'),0))OleCheck(ln.SetHotkey());OleCheck(ln.SetShowCmd());