c++ - SHGetFolderPath 已弃用 : What is alternative to retrieve path for Windows folders?

标签 c++ windows winapi known-folders

SHGetFolderPath() 函数从 Windows Vista 开始被弃用:http://msdn.microsoft.com/en-us/library/bb762181%28v=VS.85%29.aspx

在 Windows 中检索应用程序文件夹路径的替代方法是什么?

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

除此之外,为什么在使用此功能时会出现这些错误:

Error   1   error C2065: 'CSIDL_COMMON_APPDATA' : undeclared identifier 

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

最佳答案

您链接到的文档中描述了替代方法。即是SHGetKnownFolderPath .

但是,SHGetKnownFolderPath 仅在 Vista 或更高版本上可用。因此,如果您使用加载时链接,并在 XP 上运行调用 SHGetKnownFolderPath 的程序,则该程序将无法启动。如果您希望支持 XP,这显然是个问题。

现在,您可以切换到 SHGetKnownFolderPath 的运行时链接。在调用之前执行版本检查,如果该函数不可用,则回退到 SHGetFolderPath

就个人而言,我不会让这种弃用让您过分担心。 Microsoft 以保持向后兼容性而闻名。不要期望 SHGetFolderPath 会很快消失。您会发现 SHGetFolderPath 存在于 Windows 8 中,我希望它在 10 年后的任何 Windows 中仍然存在。我的建议是坚持加载时链接,只有在您放弃支持 XP 时才切换到 SHGetKnownFolderPath

您在编辑中提出的另一个问题是如何调用 SHGetFolderPath。您需要遵守您在问题中链接到的 MSDN 文档主题底部列出的要求。具体来说,包含 Shlobj.h 并将 Shlobj.lib 传递给链接器。

关于c++ - SHGetFolderPath 已弃用 : What is alternative to retrieve path for Windows folders?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18493484/

相关文章:

c++ - 我应该如何打包和解包这个 64 位整数?

c++ - 如何在 Linux 发行版上将 SFML C++ 源代码从 Linux 编译到 Windows?

windows - 为什么不能通过添加 "update_check":false in user setting file?关闭Sublime 3更新通知

windows - cmd.exe 启动时使用什么编码/代码页?

c++ - 是否有 WinXP/WinXP Embedded API 来更改区域设置(特别是 IME)?

c++ - 使用 static_cast 然后使用 dynamic_cast

c++ - std::initializer_list 和引用类型

c++ - 如何在不使用异常的情况下检查 constructor() 中的失败?

c - WinApi 基础服务教程?

c# - 通过处理 NC_HITTEST 在移动控件时设置 SizeAll 光标