user88004 user88004 - 4 months ago 49
C++ Question

SHGetFolderPath Deprecated: What is alternative to retrieve path for Windows folders?

The

SHGetFolderPath()
function is deprecated beginning with Windows Vista: http://msdn.microsoft.com/en-us/library/bb762181%28v=VS.85%29.aspx

What is the alternative way to retrieve the path to the Application Folder in Windows?

SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, szPath)


Aside from that, why do I get those errors when using this function:

Error 1 error C2065: 'CSIDL_COMMON_APPDATA' : undeclared identifier

Error 2 error C3861: 'SHGetFolderPath': identifier not found

Answer

The alternative is described in the documentation to which you link. Namely it is SHGetKnownFolderPath.

However, SHGetKnownFolderPath is only available on Vista or later. So if you use load time linking, and run a program that calls SHGetKnownFolderPath on XP, then that program will fail to start. This is clearly a problem if you wish to support XP.

Now, you could switch to run time linking of SHGetKnownFolderPath. Carry out a version check before you call it, and if the function is not available, then fall back to SHGetFolderPath.

Personally, I would not let this deprecation worry you excessively. Microsoft are renowned for maintaining backwards compatibility. Don't expect SHGetFolderPath to disappear any time soon. You will find that SHGetFolderPath exists in Windows 8 and I would expect it still to be present in whatever Windows is current 10 years from now. My advice is to stick to load time linking, and only switch to SHGetKnownFolderPath when you give up supporting XP.

Your other question, that you ask in an edit, is how to call SHGetFolderPath. You need to respect the requirements which are laid out at the bottom of the MSDN documentation topic which you linked to in your question. Specifically, include Shlobj.h and pass Shlobj.lib to the linker.