What does SHGFI_USEFILEATTRIBUTES mean?

Date:June 1, 2004 / year-entry #215
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20040601-00/?p=39073
Comments:    12
Summary: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...

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.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index