C++ WinSDK header compilation error

1

I am trying to compile a program in Visual Studio 2010 in Windows 7. I am getting a compilation error C2061: syntax error: identifier 'KNOWN_FOLDER_FLAG' in Windows header shlobj.h at the marked line:

#if (NTDDI_VERSION >= NTDDI_VISTA)

typedef enum
{
    KF_FLAG_DEFAULT         = 0x00000000,
    KF_FLAG_CREATE          = 0x00008000,
    KF_FLAG_DONT_VERIFY     = 0x00004000,
    KF_FLAG_DONT_UNEXPAND   = 0x00002000,
    KF_FLAG_NO_ALIAS        = 0x00001000,
    KF_FLAG_INIT            = 0x00000800,
    KF_FLAG_DEFAULT_PATH    = 0x00000400,
    KF_FLAG_NOT_PARENT_RELATIVE = 0x00000200,
    KF_FLAG_SIMPLE_IDLIST   = 0x00000100,
    KF_FLAG_ALIAS_ONLY      = 0x80000000,
} KNOWN_FOLDER_FLAG;


DEFINE_ENUM_FLAG_OPERATORS(KNOWN_FOLDER_FLAG);

STDAPI SHGetKnownFolderIDList(__in REFKNOWNFOLDERID rfid,
                              __in DWORD /* KNOWN_FOLDER_FLAG */ dwFlags,
                              __in_opt HANDLE hToken,
                              __deref_out PIDLIST_ABSOLUTE *ppidl);

STDAPI SHSetKnownFolderPath(__in REFKNOWNFOLDERID rfid,
                            __in DWORD /* KNOWN_FOLDER_FLAG */ dwFlags,
                            __in_opt HANDLE hToken,
                            __in PCWSTR pszPath);

STDAPI SHGetKnownFolderPath(__in REFKNOWNFOLDERID rfid,
                            __in DWORD /* KNOWN_FOLDER_FLAG */ dwFlags,
                            __in_opt HANDLE hToken,
                            __deref_out PWSTR *ppszPath);

#endif  // NTDDI_VISTA

#if (NTDDI_VERSION >= NTDDI_WIN7)

STDAPI SHGetKnownFolderItem(__in REFKNOWNFOLDERID rfid,
                            __in KNOWN_FOLDER_FLAG flags, <<<ERROR AT THIS LINE
                            __in_opt HANDLE hToken,
                            __in REFIID riid,
                            __deref_out void **ppv);
#endif // NTDDI_WIN7

In my program the version macros is defined as folows

#define NTDDI_VERSION NTDDI_WINXP

What can be the reason for it not to compile?

It seems to me that the part I am getting the error in must not be compiled at all, but it somehow does.

c++
windows
visual-studio-2010
asked on Stack Overflow May 24, 2012 by grzkv

3 Answers

1

There is only one logical conclusion: for some reason the expansion of NTDDI_VERSION when this header is compiled is not what you think it is.

Try troubleshooting by searching for #undef NTDDI_VERSION and making a clean build of your project (might be relevant if you are using precompiled headers).

answered on Stack Overflow May 24, 2012 by Jon
1

EDIT: My answer was written without scrolling down the code sample... Have you confirmed that the error is coming from the line that defines SHGetKnownFolderItem? When I run into these kinds of problems, I usually create a preprocessor output file for your file and confirm that the error is actually coming from where you expect. It's possible there is some other piece of code which wasn't properly NTDDI_VERSION guarded.

The windows SDK headers are built to allow you to build an application for a specific version of Windows.

In this case you've asked to build your application for Windows XP (by setting NTDDI_VERSION to NTDDI_WINXP). That's great. But then you're trying to use an enum (KNOWN_FOLDER_FLAG) that was introduced in Windows Vista.

You need to make a choice: Build your application using the Windows Vista version of the Windows APIs (by setting NTDDI_VERSION to NTDDI_VISTA) or figure out another way of separating the Windows Vista specific functionality.

There are a couple of ways of doing that. The first (and trickiest) is to build your app with the Vista headers and then be careful to avoid using the Vista-only functionality on XP. Alternatively, you could isolate the vista specific functionality in another source file/dll and compile that file with NTDDI_VISTA. Then only call the vista specific functionality when you're not on XP.

Note that if you add a direct call to SHGetKnownFolderPath to your application, it won't work on Windows XP - that's because that API doesn't exist on XP. So you'll have to do the LoadLibrary/GetProcAddress trick to call the API.

0

change

define NTDDI_VERSION NTDDI_WINXP

to

define NTDDI_VERSION NTDDI_VISTA

before declaration ShlObj header work fine in windows 10

answered on Stack Overflow Apr 16, 2017 by dany seban • edited Jun 20, 2020 by Community

User contributions licensed under CC BY-SA 3.0