Board index » delphi » How do I create a .LNK file?

How do I create a .LNK file?

Hi

Can anyone advise how I can create a LNK file or shortcut, (for example in
the desktop or "recent" folder) from within my code?

Thanks in advance

Trevor

 

Re:How do I create a .LNK file?


How about this :

procedure WriteShortCut (ShortCut,Application,
                         Parameters,WorkDir  : string  ;
                         SW_State            : Integer ;
                         IconFile            : string  ;
                         IconIndex           : byte    ) ;
var  SCObject  : IUnknown     ;
     SCSLink   : IShellLink   ;
     SCPFile   : IPersistFile ;
     WFName    : WideString   ;
begin
  SCObject := CreateComObject(CLSID_ShellLink);
  SCSLink  := SCObject as IShellLink          ;
  SCPFile  := SCObject as IPersistFile        ;
  SCSLink.SetPath            (PChar(Application));
  SCSLink.SetArguments       (PChar(Parameters ));
  SCSLink.SetWorkingDirectory(PChar(Workdir    ));
  SCSLink.SetShowCmd         (SW_State)          ;
  SCSLink.SetIconLocation    (PChar(IconFile),IconIndex) ;
  WFName := Shortcut ;
  SCPFile.Save (PWChar(WFName),false) ;
end;

In article <8rkb9i$ai...@ctb-nnrp2.saix.net>,
  "Trevor Davies" <tdav...@somta.co.za> wrote:

Quote
> Hi

> Can anyone advise how I can create a LNK file or shortcut, (for
example in
> the desktop or "recent" folder) from within my code?

> Thanks in advance

> Trevor

Sent via Deja.com http://www.deja.com/
Before you buy.

Re:How do I create a .LNK file?


Hi,

But where is the IShellLink declared? And the IPersistFile too for that
matter?

TIA,
Phil

"Jeroen" <jvber...@my-deja.com> schreef in bericht
news:8rkt7i$evv$1@nnrp1.deja.com...

Quote
> How about this :

> procedure WriteShortCut (ShortCut,Application,
>                          Parameters,WorkDir  : string  ;
>                          SW_State            : Integer ;
>                          IconFile            : string  ;
>                          IconIndex           : byte    ) ;
> var  SCObject  : IUnknown     ;
>      SCSLink   : IShellLink   ;
>      SCPFile   : IPersistFile ;
>      WFName    : WideString   ;
> begin
>   SCObject := CreateComObject(CLSID_ShellLink);
>   SCSLink  := SCObject as IShellLink          ;
>   SCPFile  := SCObject as IPersistFile        ;
>   SCSLink.SetPath            (PChar(Application));
>   SCSLink.SetArguments       (PChar(Parameters ));
>   SCSLink.SetWorkingDirectory(PChar(Workdir    ));
>   SCSLink.SetShowCmd         (SW_State)          ;
>   SCSLink.SetIconLocation    (PChar(IconFile),IconIndex) ;
>   WFName := Shortcut ;
>   SCPFile.Save (PWChar(WFName),false) ;
> end;

> In article <8rkb9i$ai...@ctb-nnrp2.saix.net>,
>   "Trevor Davies" <tdav...@somta.co.za> wrote:
> > Hi

> > Can anyone advise how I can create a LNK file or shortcut, (for
> example in
> > the desktop or "recent" folder) from within my code?

> > Thanks in advance

> > Trevor

> Sent via Deja.com http://www.deja.com/
> Before you buy.

Re:How do I create a .LNK file?


Quote
"Philip Blond" <pblo...@pavell.com> wrote:
>Hi,

>But where is the IShellLink declared? And the IPersistFile too for that
>matter?

Ole2.pas (i think).

Smola
--
"And once again, the day is saved!"
(supersmola.tripod.com/index.htm}

Re:How do I create a .LNK file?


In article <PbAE5.82426$nQ2.1386...@afrodite.telenet-ops.be>, "Philip Blond"

Quote
<pblo...@pavell.com> writes:
>But where is the IShellLink declared?

In D3 . . .
  ActiveX.pas

Quote
>And the IPersistFile too for that
>matter?

ShlObj.pas.

Use Windows Explorer to search for a file containing "IShellLink =" (and then
"IPersistFile =") in the Delphi Source folder and below. Similar technique for
any "Where is it declared" question.

Alan Lloyd
alangll...@aol.com

Re:How do I create a .LNK file?


"AlanGLLoyd" <alangll...@aol.com> skrev i melding
news:20001011022318.26694.00000014@nso-fo.aol.com...

Quote
> Use Windows Explorer to search for a file containing "IShellLink ="
(and then
> "IPersistFile =") in the Delphi Source folder and below. Similar
technique for
> any "Where is it declared" question.

..or use Delphi's "Find in Files" - feature. Beware of the FIFB, though.
--
Bjoerge Saether
Consultant / Developer
Asker, Norway
bsaether.removet...@online.no (remove the obvious)

Re:How do I create a .LNK file?


Quote
Trevor Davies wrote:
>Can anyone advise how I can create a LNK file or shortcut, (for example in
>the desktop or "recent" folder) from within my code?

// ===================================
// ==== from the Jedi Code Library ===
// ===================================

const
  // IID_IShellLinkA
  IID_IShellLink: TGUID = (
    D1:$000214EE;
    D2:$0000;
    D3:$0000;
    D4:($C0,$00,$00,$00,$00,$00,$00,$46));

type
  PShortCut = ^TShortCut;
  TShortCut = record
    Arguments: string;
    ShowCmd: Integer;
    WorkingDirectory: string;
    IdList: PItemIDList;
    Target: string;
    Description: string;
    IconLocation: string;
    IconIndex: Integer;
    HotKey: Word;
  end;

procedure ShortCutFree(var ShortCut: TShortCut);
begin
  PidlFree(ShortCut.IdList);
end;

function ShortCutCreateSystem(
  const ShortCut: TShortCut;
  const Folder: Integer;
  const FileName: string): HRESULT;
var
  Path: string;
begin
  Result := E_INVALIDARG;
  SetLength(Path, MAX_PATH);
  if SHGetSpecialFolderPath(0, PChar(Path), Folder, False) then
  begin
    StrResetLength(Path);
    Result :=
      ShortCutCreate(ShortCut, PathAddSeparator(Path) + FileName);
  end;
end;

function ShortCutCreate(
  const ShortCut: TShortCut;
  const FileName: string): HRESULT;
var
  ShellLink: IShellLink;
  PersistFile: IPersistFile;
  LinkName: array [0..MAX_PATH] of WideChar;
begin
  Result := CoCreateInstance(
    CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER,
    IID_IShellLink, ShellLink);
  if Succeeded(Result) then
  begin
    ShellLink.SetArguments(PChar(ShortCut.Arguments));
    ShellLink.SetShowCmd(ShortCut.ShowCmd);
    ShellLink.SetWorkingDirectory(PChar(ShortCut.WorkingDirectory));
    ShellLink.SetPath(PChar(ShortCut.Target));
    ShellLink.SetDescription(PChar(ShortCut.Description));
    ShellLink.SetHotkey(ShortCut.HotKey);
    ShellLink.SetIconLocation(
      PChar(ShortCut.IconLocation), ShortCut.IconIndex);
    PersistFile := ShellLink as IPersistFile;
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PChar(FileName), -1,
      LinkName, MAX_PATH);
    Result := PersistFile.Save(LinkName, True);
  end;
end;

function ShortCutResolve(
  const FileName: string;
  var ShortCut: TShortCut): HRESULT;
var
  ShellLink: IShellLink;
  PersistFile: IPersistFile;
  LinkName: array [0..MAX_PATH] of WideChar;
  Buffer: string;
  Win32FindData: TWin32FindData;
begin
  Result := CoCreateInstance(
    CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER,
    IID_IShellLink, ShellLink);
  if Succeeded(Result) then
  begin
    PersistFile := ShellLink as IPersistFile;
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PChar(FileName), -1,
      LinkName, MAX_PATH);
    Result := PersistFile.Load(LinkName, STGM_READ);
    if Succeeded(Result) then
    begin
      Result := ShellLink.Resolve(0, SLR_ANY_MATCH);
      if Succeeded(Result) then
      begin
        SetLength(Buffer, MAX_PATH);
        ShellLink.GetPath(
          PChar(Buffer), MAX_PATH, Win32FindData, SLGP_SHORTPATH);
        ShortCut.Target := PChar(Buffer);
        ShellLink.GetArguments(PChar(Buffer), MAX_PATH);
        ShortCut.Arguments := PChar(Buffer);
        ShellLink.GetShowCmd(ShortCut.ShowCmd);
        ShellLink.GetWorkingDirectory(PChar(Buffer), MAX_PATH);
        ShortCut.WorkingDirectory := PChar(Buffer);
        ShellLink.GetDescription(PChar(Buffer), MAX_PATH);
        ShortCut.Description := PChar(Buffer);
        ShellLink.GetIconLocation(
          PChar(Buffer), MAX_PATH, ShortCut.IconIndex);
        ShortCut.IconLocation := PChar(Buffer);
        ShellLink.GetHotkey(ShortCut.HotKey);
        ShellLink.GetIDList(ShortCut.IdList);
      end;
    end;
  end;
end;

Regards,
Udo
--
Homepage: http://www.nesshoever.net/
Reply to newsgroup. Unrequested mails will be ignored.

Other Threads