December 19th, 2012

Why doesn't SHGetKnownFolderPath return the customized path?

A customer reported that the SHGet­Known­Folder­Path function was not working correctly. They moved their Videos folder to a new location, but when they called SHGet­Known­Folder­Path, they got the old path rather than the new one.

A quick check of the code they provided showed why:

 ...
 SHGetKnownFolderPath(FOLDERID_Videos,
                      KF_FLAG_DONT_VERIFY |
                      KF_FLAG_DEFAULT_PATH,
                      NULL,
                      &pszPath);

Um, you’re passing the KF_FLAG_DEFAULT_PATH flag. That flag means “Tell me where this folder would have been if its location had never been customized.” Therefore, if you pass this flag, you have no right to complain that it’s returning a path different from the customized path. Because passing that flag means “I don’t want the customized path.”

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.