February 15th, 2016

How can I get the name for the user’s profile directory that is shown in Explorer?

A customer wanted to know how they could get the SHGet­Localized­Name function to work. Fortunately, they gave a specific definition of “work”.

We are populating a list of folders, and we want the names to be the same names that appear in Explorer. We are diong it by calling SHGet­Localized­Name, and if it succeeds, doing a Load­Library­Ex with LOAD_LIBRARY_AS_DATAFILE, and then a Load­String. (If the call to SHGet­Localized­Name fails, then we just use the raw file system name.) This works for the most part, but not for the user profile. How can we get the correct name for the user profile folder?

The user profile name is not a localized name, which is why SHGet­Localized­Name doesn’t give you any information about it. Instead, that name is dynamically generated based on the user whose profile you are looking at. (It’s not like there’s a giant usernames.dll which includes the name of every possible user on the planet, and SHGet­Localized­Name says “Oh, yeah, the user is Raymond Chen? That’s string number 31415 in usernames.dll.”)

The way to get the name that Explorer displays for an item is to ask the same data source that Explorer uses: the shell namespace.

And hey, how about that, I already wrote a sample program that shows how to do this, just in the context of a different question, so I’m going to incorporate the existing program by reference. Here you go.

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.