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).
Update: The idea is that they are showing a file listing unrelated to the file system. For example, they are an online photo album browser. They want to show all the photos in the current album, plus an icon that means “Go to the parent folder.” End update
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 is recognized as a relative path and is resolved against the current directory. And then you get the icon for whatever that turns out to be.
Resulting iconfolderdrive
| Current directory | C:\repos\contoso | C:\contoso |
|---|---|---|
| ".." resolves to | C:\repos | C:\ |
So what should you do if you want to get a folder icon?
You can pass the SHGFI_ flag, which means, “Just pretend that I gave you a valid path to a thing with the attributes I passed as the second parameter. What icon would you use for it?”
SHGetFileInfo(L"C:\\a", FILE_ATTRIBUTE_DIRECTORY, &info, sizeof(info), SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_SMALLICON | SHGFI_SYSICONINDEX);
We pass a file path of C:\a and say that its attributes are FILE_. The actual path is not important, as long as it is a syntactically valid path.
Next time, we’ll look at a follow-up question related to this problem.
¹ I want to remind you that if you force me to do psychic debugging, then you have failed. When I coined the term “psychic debugging”, I used it to describe the case of debugging a problem where the person asking the question omitted crucial information, forcing me to go to the extra effort of obtaining that information through supernatural means. It’s like showing up at the passport office without having the proper forms filled out. The clerk can probably give you the correct form and help you fill it out, but they’re not going to be happy about it because you’re just creating extra work for them due to being ill-prepared.
Those invisible U+00AD inside SHGFI_USEFILEATTRIBUTES in the post are really annoying. Just lost 5 minutes trying to search the flag in my code base 🙁 The old article (https://devblogs.microsoft.com/oldnewthing/20040601-00/?p=39073/) luckily didn’t have those.
Mr. Chen adds those intentionally to help with word breaking, I guess because a pure HTML-based solution doesn’t play well with the blog migrating to a different system every year.
I feel like I’m missing something. If you’re only one level into the directory tree, and so
..means the root of the drive, wouldn’t have the icon be the icon for the drive itself be correct?Even if this application were Explorer and it were to include “..” in the directory listing, I think that I would want the icon which represents navigation to a parent. The return arrow used by Program Manager seems appropriate here. I do not think I would want that icon to be the icon that would be shown for the parent folder in the parent”s parent folder.
Regardless of whether or not I agree with the program design, it is cool to know there is an API to actually specifically request a generalized folder icon!
They didn’t want the icon for “the parent of the process current directory”. They wanted an icon that they could use generically throughout their program to represent “a” parent folder, not any specific parent folder. For example, maybe they are a program for browsing an online photo album, and they want an icon to mean “Browse the parent folder of the folder you are currently looking at.” There’s no file system at all. They just want a generic “parent folder” icon. (I’ll update the article to clarify.)
The dev probably intended the .. to be from some directory the application was working with, but forgot to prepend that path. E.g. if the app is showing a listing for the user’s Documents folder, they should have taken that path, made sure it ends with a backslash, and then appended .. to it, then passed it to the function for getting the icon. But instead they just passed plain .. on its own, which as explained resolves relative to the working directory, which could be anything.EDIT: My theory is wrong, see Mr. Chen’s comment.I see. Well yeah, if you don’t specify what your relative path is relative to, then it will be relative to something else you don’t know about. It seems to me that that’s the problem, not the icon-generation logic. The icon should be based on the actual object it would navigate to, and not just “What would any arbitrary folder’s icon be”. It just seems like this post might be answering the wrong question. But maybe I’m just taking the motivating example too literally.