June 1st, 2004

What does SHGFI_USEFILEATTRIBUTES mean?

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.