【问题标题】:Targetting Windows 7 desktop: identifier "SHGetKnownFolderPath" is undefined以 Windows 7 桌面为目标:标识符“SHGetKnownFolderPath”未定义
【发布时间】:2016-03-08 04:01:08
【问题描述】:

我正在为 Windows 7 及更高版本编写 C++ 桌面应用程序。
我想获取 AppData/Roaming 文件夹的路径,所以我使用 SHGetKnownFolderPath:

#include "stdafx.h"
#include <windows.h>
#include <ShlObj.h>

void hello()
{
    LPWSTR roamingPath;
    SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &roamingPath);

问题:构建失败并出现identifier "SHGetKnownFolderPath" is undefined,这很奇怪,因为我认为我包含了正确的标题。


注意事项:

  • Visual Studio 2015 告诉我我的编译选项是 /Yu"stdafx.h" /GS /analyze- /W3 /Zc:wchar_t /ZI /Gm /Od /Fd"Debug\vc140.pdb" /Zc:inline /fp:precise /D "WIN32" /D "_WINDOWS" /D "_DEBUG" /D "_USRDLL" /D "_WINDLL" /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\OverlayIcon.pch"
  • Error: identifier :"SHGetKnownFolderPath" is unidentified 不同,问题在于提问者的目标不仅仅是桌面。

【问题讨论】:

  • 您的 stdafx.h 标头中的 target platform macros 可能对这个问题很重要,因为它们的值表明在通过标头编译时公开了哪些功能。可能希望在您的问题中包含这些内容。
  • @WhozCraig:添加! StackOverflow 语法问题:-/
  • 在您更新帖子后,它现在应该至少编译源代码。顺便说一句,您在项目中拥有该 stdafx.h 标头。不只是将值更改为正确的目标没有什么意义,因为它无论如何都包含在您的所有翻译单元中。

标签: c++ visual-studio-2015 header-files appdata shell32.dll


【解决方案1】:

诀窍是在您的stdafx.h 文件中添加这两行:

#define WINVER 0x0601 // Allow use of features specific to Windows 7 or later.
#define _WIN32_WINNT 0x0601

这表示该应用程序面向 Windows 7,这很重要,因为 SHGetKnownFolderPath 仅在 Windows Vista 中可用,如 MSDN documentation 中所指定。它对我来说并没有立即起作用,我不得不清理甚至重新启动 Visual Studio。

以下是所有其他 Windows 版本的代码:
https://msdn.microsoft.com/en-us/library/6sehtctf.aspx

感谢 WhozCraig 的提示!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多