Tuning SHGetFileInfo for Optimum Performance

If you're writing programs to interface with the Windows Shell, chances are you'll eventually want to use the SHGetFileInfo function. This function "retrieves information about an object in the file system, such as a file, a folder, a directory, or a drive root". However, this information may come at a heavy price.

Recently, I wrote an Explorer style "Open file" dialog that used a CListCtrl. The CListCtrl was populated by enumerating the Windows Shell for a specified directory. I needed to retrieve the icon and the type name for each shell object. The dialog performed fine until I enumerated a directory with several thousand files. The total time required to fill the control ranged from 20-40 seconds. The code I used included the following snippet:

//  get the shell object info.
SHGetFileInfo(path,
              0,
              &sfi,
              sizeof(SHFILEINFO), 
              SHGFI_ICON | SHGFI_TYPENAME);

After using the Microsoft Profiler (within the development environment), it was obvious that a good majority of the time was spent executing the SHGetFileInfo command. A command I thought would execute fairly quickly. What I discovered is that using the above format of the command forced the program to access each file in the enumeration to obtain the requested information. This can be a performance hit when you're accessing several thousand files over a network. Microsoft's documentation does not clearly explain this.

The resolution to my problem came about after much research and reading of Dino Esposito's Visual C++ Windows Shell Programming. I needed a way of obtaining the required information WITHOUT accessing every file in the enumeration. The resolution required the following change to my code:

//  get attributes of the shell object using its pidl.
psfFolder->GetAttributesOf(1, (LPCITEMIDLIST*)&pIDL, &dwFileAttr);

//  set the SHGetFileInfo attribute based on the type
//  of shell object.  We'll use this to force the
//  SHGetFileInfo call to NOT access the shell object.
//  (all we want is the icon & type name; the 
//  combination of the SHGFI_USEFILEATTRIBUTES flag
//  and the FILE_ attribute will prevent any unnecessary
//  access of the shell object).
if ((dwFileAttr & SFGAO_FOLDER) == SFGAO_FOLDER)
 attr = FILE_ATTRIBUTE_DIRECTORY;
else
 attr = FILE_ATTRIBUTE_NORMAL;

//  get the shell object info.
SHGetFileInfo(path,
              attr,
              &sfi,
              sizeof(SHFILEINFO), 
              SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_TYPENAME);

By setting the attr variable to a valid FILE_ flag and adding the SHGFI_USEFILEATTRIBUTES flag, I was able to avoid accessing each file in the enumeration. By setting the SHGFI_USEFILEATTRIBUTES flag, I forced the function to assume that the file passed in through the "path" variable exists. An undocumented feature allows the function to use the extension and search the registry for information about the icon and the type name. This simple change reduced my total access time to 5-6 seconds. Although not up to par with Explorer, it's a change I can live with.



Comments

  • It works fine

    Posted by Legacy on 06/01/2001 12:00am

    Originally posted by: Yarp

    I had just noticed this problem on my File Explorer. The tuning works fine.
    Thanks a lot

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds