Martin Reiner
Martin Reiner

Reputation: 2197

How to get shortcut shell icon from a system shortcut?

Some time ago I've asked for a fastest way to get shell icon what tuned the speed of getting icons up, but the problem is that some of the system shortcuts (lnk files) doesn't display correct icons.

As one of the examples might be the Windows Fax and Scan.lnk file, which if you use the following code...

procedure TForm2.Button1Click(Sender: TObject);
var
  Icon: TIcon;
  FileInfo: TSHFileInfo;
begin
  FillChar(FileInfo, SizeOf(FileInfo), 0);
  SHGetFileInfo(PChar('C:\Windows Fax and Scan.lnk'), FILE_ATTRIBUTE_NORMAL, 
    FileInfo, SizeOf(FileInfo), SHGFI_USEFILEATTRIBUTES or SHGFI_ICON or
    SHGFI_SMALLICON);
  Icon := TIcon.Create;
  try
    Icon.Handle := FileInfo.hIcon;
    Canvas.Draw(10, 10, Icon);
  finally
    Icon.Free;
  end;
end;

shows instead of this icon...

enter image description here

some kind of a default shortcut icon...

enter image description here

Do you know how to get the system icon (the one from the first image) from that shortcut file ?

Upvotes: 2

Views: 1235

Answers (1)

Anders
Anders

Reputation: 101756

SHGFI_USEFILEATTRIBUTES tries to not access the disk, this means you can tell it to look up a.txt without that file actually existing and still give you the generic icon for .txt files. It would probably not give the correct result with something like this installed which requires the custom icon handler to be invoked and maybe accessing the disk. Since the .lnk file stores the location of the icon you cannot access this information without accessing the disk (excluding a possible cache).

IExtractIcon is faster than SHGetFileInfo according to MSDN but if you really want speed you should display generic icons and get the real icons on a background thread...

Upvotes: 1

Related Questions