针对 Windows 7 桌面:标识符"SHGetKnownFolderPath"未定义

Targetting Windows 7 desktop: identifier "SHGetKnownFolderPath" is undefined

本文关键字:SHGetKnownFolderPath 未定义 标识符 Windows 桌面 针对      更新时间:2023-10-16

我正在为 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"Debugvc140.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"DebugOverlayIcon.pch"
  • 与错误不同:标识符:"SHGetKnownFolderPath"未识别,问题在于提问者的目标不仅仅是桌面。
诀窍

是在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文档中指定的那样。它对我不起作用,我不得不清理甚至重新启动Visual Studio。

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

感谢WhozCraig的提示!

相关文章:
  • 没有找到相关文章