How can I know whether the next SHGetFileInfo (SHGFI_ICON) call will be slow (for exe files etc.)?

StackOverflow https://stackoverflow.com/questions/23077457

  •  03-07-2023
  •  | 
  •  

Question

I am writting a file manager. When it opens, the UI freezes for a while. I profiled it and find the slow function is SHGetFileInfo.

SHFILEINFO shinfo;
SHGetFileInfo(FullPath.c_str(), NULL, &shinfo, sizeof(shinfo),
    SHGFI_ICON | SHGFI_LARGEICON))

Also, the program runs fast enough even when there are hundreds of folders, but becomes extremely slow when there is at least one executable. I think loading icon from such files is a heavy task since the resources must be loaded.

I see that when Windows Explorer loads a folder, all icons except executables are correct, and executables have their default icons (the icon of command line utilities). Then their icons become correct in a few seconds.

My question is how can Explorer determine which icons are fast to load and which are slow? I think checking for the extension .exe is not reliable because one may do assoc .foo=exefile or set another extension's icon to %1, etc.

Was it helpful?

Solution

The Windows shell uses a couple of mechanisms to deal with this performance hit:

  1. It looks up icons in a background thread. Explorer is still perfectly usable whilst this thread is doing its work.
  2. Explorer caches icons. This means that the icon extraction is performed once only and the result is remembered.

I think if you want your file manager to be as usable as Explorer then you will need to implement similar optimisations. Or perhaps SHGetFileInfo already takes advantage of the system icon cache.

These articles relating to shell icon performance may be useful to you:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top