4

(Visual Studio 2010 / Visual C++ / Windows 7)

example.cpp:

#include <Shlobj.h>
#pragma comment (lib, "Shell32.lib")    
...
void example()
{
    SHGetKnownFolderPath(...) // undefined
}

I'm doing everything according to documentation and what I see in other threads, but it still doesn't work.

joe
  • 8,344
  • 9
  • 54
  • 80
paperduck
  • 1,175
  • 1
  • 16
  • 26
  • What do you mean? What happens when you compile the code? When you run it? Why aren't you passing the parameters to the function that it expects? Try showing us your actual code instead of pseudocode. – jalf Mar 13 '14 at 12:51
  • What's the exact error? You might not be specifying Vista+. – chris Mar 13 '14 at 12:51
  • @sgar91, I know from experience that including the .lib works. – chris Mar 13 '14 at 12:55
  • @chris, error C3861: 'SHGetKnownFolder': identifier not found – paperduck Mar 13 '14 at 12:58
  • 3
    Please don't ever say, "it does not work". Describe in full the failure mode, including verbatim error messages. – David Heffernan Mar 13 '14 at 13:05
  • I know this is old, but did you ever figure out what the issue was? – WBuck Aug 31 '18 at 21:10
  • @WBuck It was a long time ago so I'm not sure. It may have been an issue with escaping special characters like backslashes. – paperduck Sep 02 '18 at 00:24

2 Answers2

3

I had exactly the same problem. Another project with the same code and ancillary files (but different includes) was working.

Putting #include <Shlobj.h> at the top of the file solved the problem.

It might not be replicable though, as it should have worked without doing that. Probably another Visual Studio bug.

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • If changing the order of includes appears to fix an issue, then the issue is either related to the header files involved (*Shlobj.h* is not part of Visual Studio, btw.), or not adhering to additional rules when using precompiled header files. Certainly not a bug in Visual Studio. – IInspectable Jul 23 '16 at 14:15
2

Try putting following statement before all includes:

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600

Since the documentation says it needs Vista/2008 minimum.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
Ajay
  • 18,086
  • 12
  • 59
  • 105
  • 3
    You've got Windows 8 there. Vista is 0x0600 and 7 (the OP's) is 0x0601. Alternatively, there's `_WIN32_WINNT_VISTA` and `_WIN32_WINNT_WIN7`. – chris Mar 13 '14 at 13:16
  • Yup, I have put the latest one, which I use! – Ajay Mar 13 '14 at 13:21