Technology1 min read

Why do I get inconsistent results when I ask for the icon for the .. parent folder?

By · Published by Everything Blog

In short

The SHGetFileInfo function interprets the double-dot entry as a relative path resolved directly against the application's current working directory. When executed from a directory nested two levels down, the resolved path points to a regular folder, whereas running one level down targets the drive root. Developers can resolve this discrepancy by passing the SHGFI_USEFILEATTRIBUTES flag paired with the FILE_ATTRIBUTE_DIRECTORY attribute alongside a syntactically valid path.

Key points

  • Shell icon lookups for relative parent entries depend directly on the active working dire…: Shell icon lookups for relative parent entries depend directly on the active working directory of the application.
  • Resolving the relative path against a shallow directory hierarchy causes the operating sy…: Resolving the relative path against a shallow directory hierarchy causes the operating system to return a drive icon.
  • Developers can guarantee a standard folder icon by combining the SHGFI_USEFILEATTRIBUTES…: Developers can guarantee a standard folder icon by combining the SHGFI_USEFILEATTRIBUTES flag with the FILE_ATTRIBUTE_DIRECTORY attribute.

Sources checked by our analysis pipeline: 1

A customer wanted their program to show a file listing with icons, and they also wanted to include an entry for the parent folder, programmatically known as .. (double period). They obtained icons by calling the SHGetFileInfo

function, but they got weird, inconsistent results when they asked for the icon of the .. parent folder.

SHGetFileInfo(L"..", 0, &info, sizeof(info), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_SYSICONINDEX);

They found that when they ran the program in a debugger, it gave them the desired folder icon 🗀. But when it ran on the customer’s system, it showed a drive icon 🖴. What’s going on? Why is the icon changing depending on who is running the program?

My psychic debugging powers¹ told me that the developer told their debugger to run the program with the current directory equal to something that is at least two directory levels away from the root. Perhaps they are using Visual Studio and accepted the default configuration that sets the current directory to $(ProjectDir), and their project directory is eomthing like C:\

When you pass ".." to SHGetFileInfo

, it…

Technology