How to create a file shortcut (*.lnk file) on desktop in Windows?
Asked Answered
D

3

11
function GetDesktopFolder: string;
var
  buf: array[0..MAX_PATH] of Char;
  pidList: PItemIDList;
begin
  Result := StrNoDesktopFolderFo;
  SHGetSpecialFolderLocation(Application.Handle, CSIDL_DESKTOP, pidList);
  if (pidList <> nil) then
    if (SHGetPathFromIDList(pidList, buf)) then
      Result := buf;
end;

procedure p;
var
  i: Integer;
  IObject: IUnknown;
  ISLink: IShellLink;
  IPFile: IPersistFile;
  PIDL: PItemIDList;
  InFolder: array[0..MAX_PATH] of Char;
  TargetName: string;
  LinkName: string;
begin
  TargetName := 'c:\folder\exeFile.exe';//hardcoded example

  IObject := CreateComObject(CLSID_ShellLink) ;
  ISLink := IObject as IShellLink;
  IPFile := IObject as IPersistFile;

  with ISLink do
  begin
    SetDescription('what ever');
    SetPath(pChar(TargetName)) ;
    SetWorkingDirectory(pChar(ExtractFilePath(TargetName))) ;
  end;

  SHGetSpecialFolderLocation(0, CSIDL_DESKTOPDIRECTORY, PIDL) ;
  SHGetPathFromIDList(PIDL, InFolder) ;

  LinkName := getDesktopFolder+'\';
  i := ;

  LinkName:= linkname+ExtractFileName(TargetName)+'.lnk';

  if LinkName = StrNoDesktopFolderFo then
    Exit;
  if not FileExists(LinkName) then
    IPFile.Save(PWChar(LinkName), False);

  Application.Terminate;
end;

The above code causes a lot of errors in Delphi and cannot run twice...

Any ideas ?

Btw. the source is not originally mine, it was picked up from places on the web.

Degenerate answered 9/10, 2011 at 14:39 Comment(1)
not the problem but your arrays should be 0..MAX_PATH-1Callus
D
14

I would do it e.g. this way

uses
  ShlObj, ComObj, ActiveX;

function GetDesktopFolder: string;
var
  PIDList: PItemIDList;
  Buffer: array [0..MAX_PATH-1] of Char;
begin
  Result := '';
  SHGetSpecialFolderLocation(Application.Handle, CSIDL_DESKTOP, PIDList);
  if Assigned(PIDList) then
    if SHGetPathFromIDList(PIDList, Buffer) then
      Result := Buffer;
end;

function CreateDesktopShellLink(const TargetName: string): Boolean;
var
  IObject: IUnknown;
  ISLink: IShellLink;
  IPFile: IPersistFile;
  PIDL: PItemIDList;
  LinkName: string;
  InFolder: array [0..MAX_PATH-1] of Char;
begin
  Result := False;

  IObject := CreateComObject(CLSID_ShellLink);
  ISLink := IObject as IShellLink;
  IPFile := IObject as IPersistFile;

  with ISLink do
  begin
    SetDescription('Description ...');
    SetPath(PChar(TargetName));
    SetWorkingDirectory(PChar(ExtractFilePath(TargetName)));
  end;

  SHGetSpecialFolderLocation(0, CSIDL_DESKTOPDIRECTORY, PIDL);
  SHGetPathFromIDList(PIDL, InFolder) ;

  LinkName := IncludeTrailingBackslash(GetDesktopFolder);
  LinkName := LinkName + ExtractFileName(TargetName) + '.lnk';

  if not FileExists(LinkName) then
    if IPFile.Save(PWideChar(LinkName), False) = S_OK then
      Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if CreateDesktopShellLink('C:\Folder\ExeFile.exe') then
    ShowMessage('Link has been created ...');
end;
Dynamite answered 9/10, 2011 at 15:8 Comment(1)
I think you made a mistake by casting a sting into pwidechar... It was not working until I added this: LinkName: string; WLinkName: widestring; myWideCharPtr : PWideChar; InFolder: array [0..MAX_PATH-1] of Char; and then WLinkName := LinkName; myWideCharPtr := Addr(WLinkName[1]); if not FileExists(LinkName) then if IPFile.Save(myWideCharPtr, False) = S_OK then Result := True;Acceleration
C
0

Functions for obtaining special folder locations, creating shortcuts (links), and much much more can be found in the freeware dsiWin32 library of 100+ procedures and functions. Download it at http://gp.17slon.com/gp/dsiwin32.htm and thanks to Primoz Gabrijelcic and the Delphi-SI community for making it available.

Max

Connett answered 10/10, 2011 at 9:58 Comment(0)
I
0
uses ShlObj, ActiveX, ComObj;
...
procedure TForm1.Button1Click(Sender: TObject);
var
IObject : IUnknown;
ISLink : IShellLink;
IPFile : IPersistFile;`enter code here`
PIDL : PItemIDList;
InFolder : array[0..MAX_PATH] of Char;
TargetName : String;
LinkName : WideString;
begin
TargetName := 'C:\Windows\System32\calc.exe';

{Use TargetName:=ParamStr(0) which
returns the path and file name of the
executing program to create a link to your
Application}

IObject := CreateComObject(CLSID_ShellLink);
ISLink := IObject as IShellLink;
IPFile := IObject as IPersistFile;

with ISLink do begin
SetPath(pChar(TargetName));
SetWorkingDirectory
(pChar(ExtractFilePath(TargetName)));
end;

// if we want to place a link on the Desktop
SHGetSpecialFolderLocation
(0, CSIDL_DESKTOPDIRECTORY, PIDL);
SHGetPathFromIDList
(PIDL, InFolder);

{
or if we want a link to appear in
some other, not-so-special, folder:
InFolder := 'c:\SomeFolder'
}

LinkName := InFolder + '\Delphi Created Link.lnk';
IPFile.Save(PWChar(LinkName), false);
end;

Source :[http://www.delphipages.com/forum/showthread.php?t=46623][1]

Interlace answered 8/4, 2019 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.