SHGetKnownFolderPath was not declared in this scope

3.1k Views Asked by At

I've been studying Java-programming for about 7 months now and I've become interested in c++. I'm currently reading a c++ book too.

I'm using eclipse c++ since I'm pretty familiar with eclipse.

I have made like 6 projects (small ones) in c++ and everything has worked fine until now.

My problem is that I can not get the SHGetKnownFolderPath method to work. The complete line is red , eventhough I have imported everything, build it, tried to run it. I have checked on internet sites and I've used the same code as other people has, but still not working for me.

It says: Function SHGetKnownFolderPath could not be resolved

I'm on a windows 8 computer 64 bit. Here is the code: UPDATE

    #define WINVER 0x0600 // 0x06020000  0x06030000
#include <shlobj.h>
#include <windows.h>
#include <combaseapi.h>
#include <comutil.h> //for _bstr_t (used in the string conversion)
#include <knownfolders.h>
#include <winerror.h> //for HRESULT
#include <winnt.h>
#include <iostream>
#include <string>






using namespace std;

int main(){

    LPWSTR wszPath = NULL;
    HRESULT hr;

    hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &wszPath);// THIS LINE IS COMPLETELY RED

    if (SUCCEEDED(hr)){
    _bstr_t bstrPath(wszPath);
    std::string strPath((char*)bstrPath);
    std::cout << strPath;
    }

    CoTaskMemFree(wszPath);

return 0;
}

Here is the log:

     #pragma comment(lib, "comsuppw")
 ^
..\src\HelloWorld.cpp: In function 'int main()':
..\src\HelloWorld.cpp:21:64: error: 'SHGetKnownFolderPath' was not declared in this scope
 hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &wszPath);

There is a little arrow pointing at the last parenthesis under &wszPath)

What could possibly be wrong? I will be grateful for all answers or hints that I could get.

2

There are 2 best solutions below

12
On BEST ANSWER

Nothing wrong with Eclipse, it is correctly processing the error output from the compiler.

Nothing wrong with the compiler, it is correctly complaining about an attempt to use a function without a preceding declaration.

The official documentation clearly tells you where to get that declaration:

enter image description here

Since this function requires Vista or later, you'll also need to follow the instructions at Using the Windows Headers for setting compatibility with a particular Windows version.

#define WINVER 0x0600
#include <windows.h>
#include <shlobj.h>

Once you fix your includes (not imports! C++ is not Java.) you'll then discover that having the ability to cast does not make type safety issues go away. This code is horribly broken:

_bstr_t bstrPath(wszPath);
std::string strPath((char*)bstrPath);

Casting a UTF-16 string to char* does not get you an ASCII string. You can either use wcout which understands UTF-16, or call WideCharToMultiByte to get an ASCII string that cout can accept.

5
On

The problem is you're using WINVER, when you should be using _WIN32_WINNT. WINVER mainly only affects very old features -- you'll generally want to define them both.

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600