One of the flags you can pass to the SHGetFileInfo function is SHGFI_USEFILEATTRIBUTES. What does this flag mean?

It means, "Do not access the disk. Pretend that the file/directory exists, and that its file attributes are what I passed as the dwFileAttributes parameter. Do this regardless of whether it actually exists or not."

You can use this flag to get the icon for a file type. For example, to get the icon for text files, pass a filename of "x.txt", pass SHGFI_USEFILEATTRIBUTES in the uFlags, and specify FILE_ATTRIBUTE_NORMAL as the file attributes.

Why is the filename "x.txt"? Because you want it to have the extension ".txt", and you want it to be a valid filename. (Don't pass illegal filenames like "???.txt".)

Why are the file attributes FILE_ATTRIBUTE_NORMAL? Because you want SHGetFileInfo to pretend that it is a normal file. (If you passed, say, FILE_ATTRIBUTE_DIRECTORY, then you would have gotten the folder icon back, since you told the function to pretend that "x.txt" was a directory.)

Note that since you are telling SHGetFileInfo to "pretend", there are some things you lose by the nature of make-believe.

For example, if the extension is a filetype whose icon changes depending on the contents of the file, then the dynamic icon will not be returned since there is no file. You told the function to "pretend", after all, and a pretend file has no contents.

Comments (12)
  1. Jack Mathews says:

    This was so handy for a program where I needed to enumerate all of the file types in the system way back when.

    Just go through .* in HKCR, get the icon for that type, then map them to their proper names and display them. I was so happy the day I found this function :)

  2. Henk Devos says:

    Are you sure that you can’t pass "???.txt"?

    I would usually pass "*.txt" to this function and that seems to work.

  3. njkayaker says:

    Too bad there isn’t a way of doing something similar to get the executable used to edit the file (sort of like FindExecutable).

  4. njkayaker says:

    Then, again, maybe AssocQueryString would work for that.

  5. Raymond Chen says:

    Henk: It may work today but it won’t work in Longhorn. The first parameter is supposed to be a valid filename.

    njkayaker: Yes I believe a variation on AssocQueryString will do the trick.

  6. J. Edward Sanchez says:

    How about just ".txt"? It’s a valid filename, after all. Even though Explorer won’t let you create it, Explorer will correctly recognize it as having the ".txt" extension (and a blank name).

    If it works, then you’ve saved yourself a whopping full character of unnecessary bloat!

  7. Len Weaver says:

    Hey Ray, could you give us an example of a filetype whose icon changes depending upon its contents? I’ve never heard of such a thing before. Thanks.

  8. Henk Devos says:

    Good that you clarified that.

    I don’t remember where i got the "*.txt", but i certainly didn’t invent this myself, and i think it is widely used like this.

  9. Centaur says:

    Some time ago, in times before XP and Me, there was a “tip” that had you set your icon for .bmp to %1. It gave you a slowdown on directories that contained many bitmaps, but you got to know just by looking what a picture was about.

  10. Raymond Chen says:

    Sure you have – you just don’t realize it. *.lnk files for example. *.exe for another. *ico for a third.

  11. Mike Dunn says:

    Setting the icon to %1 only works if there is an icon handler shell extension registered for that file type.

  12. Getting the icon for a given file extension

Comments are closed.

Skip to main content