Ivan Mark
Ivan Mark

Reputation: 463

Substitute for SHGetFileInfoW function

I'm having problem with SHGetFileInfoW function I'm using.

It's a quite slow and first read on startup (Initialization) consumes 100ms.

In MSDN stays that it should be read from thread, not the main thread because it can stuck process.

I want to use some other function, if there is any, in order to read Icons.

Another thing. How is possible to read big icons, currently I can read up to 32x32 (SHGFI_LARGEICON)

Thanks!

Actual code:

procedure TForm1.LoadIcons;
var
  Info:     TShFileInfo;
  Icon:     TIcon;
  Flags:    UINT;
  FileName: PAnsiChar;

begin
  FileName := '.txt';
  Flags := SHGFI_USEFILEATTRIBUTES or SHGFI_ICON or SHGFI_LARGEICON;
  Icon := TIcon.Create;
  try
    SHGetFileInfo(FileName, FILE_ATTRIBUTE_NORMAL, Info,
      SizeOf(Info), Flags);
    Icon.Handle := Info.hIcon;
    Image1.Picture.Assign(Icon);
    Image1.Refresh;
  finally
    DestroyIcon(Info.hIcon);
    Icon.Free;
  end;
end;

Upvotes: 1

Views: 1105

Answers (1)

kobik
kobik

Reputation: 21252

You could find the DefaultIcon for a given file extension from the Registry and use ExtractIconEx. Here is an example

I don't know if it's faster than SHGetFileInfo

EDIT:

I have extracted (from the sample) the part which gets the ICON from the Extension. It actually works very fast. could be optimized more. (I modified the code a bit):

// find the icon for a certain file extension in the registry
function TForm1.RegistryIconExtraction(Extension : string): integer;
var
    RegKey : TRegistry;
    IconPos : integer;
    AssocAppInfo : string;
    ExtractPath, FileName : string;
    IconHandle, PLargeIcon, PSmallIcon : HICON;
    AnIcon : TIcon;

begin
  Result := 0; // default icon

  if Extension[1] <> '.' then Extension := '.' + Extension;

  RegKey := TRegistry.Create(KEY_READ);
  try
    // KEY_QUERY_VALUE grants permission to query subkey data.
    RegKey.RootKey := HKEY_CLASSES_ROOT; // set folder for icon info lookup
    if RegKey.OpenKeyReadOnly(Extension) then // extension key exists?
    try
      AssocAppInfo := RegKey.ReadString('');  // read app key
      RegKey.CloseKey;
    except
      Exit;
    end;
    if ((AssocAppInfo <> '') and  // app key and icon info exists?
      (RegKey.OpenKeyReadOnly(AssocAppInfo + '\DefaultIcon'))) then
    try
      ExtractPath := RegKey.ReadString(''); // icon path
      RegKey.CloseKey;
    except
       Exit;
    end;
  finally
    RegKey.Free;
  end;

  // IconPos after comma in key  ie: C:\Program Files\Winzip\Winzip.Exe,0
  // did we get a key for icon, does IconPos exist after comma seperator?
  If ((ExtractPath <> '') and (pos(',', ExtractPath) <> 0)) then
  begin

    // Filename in registry key is before the comma seperator
    FileName := Copy(ExtractPath, 1, Pos(',', ExtractPath) - 1);
    // extract the icon Index from after the comma in the ExtractPath string
    try
      IconPos := StrToInt(copy(ExtractPath, Pos(',', ExtractPath) + 1,
        Length(ExtractPath) - Pos(',', ExtractPath) + 1));
    except
      Exit;
    end;

    IconHandle := ExtractIconEx(PChar(FileName), IconPos, PLargeIcon, PSmallIcon, 1);

    If (PLargeIcon <> 0) then
    begin
      AnIcon := TIcon.Create;
      AnIcon.Handle := PLargeIcon;

      Image1.Picture.Assign(AnIcon);
      Image1.Refresh;

      AnIcon.Free;
    end;

    DestroyIcon(PLargeIcon);
    DestroyIcon(PSmallIcon);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  t1, t2: DWORD;
begin
  t1 := GetTickCount;
  RegistryIconExtraction('.txt');
  t2 := GetTickCount;
  Memo1.Lines.Add(IntToStr(t2-t1));
end;

EDIT2: The sample code is now Vista/Win7 UAC compliant.

Upvotes: 4

Related Questions