Search code examples
c++apiwstringc2664

C++: using std::wstring in API function


I'm using the SHGetSpecialFolderLocation API function. My application is set to "Use Unicode Character Set".

Here's what I have so far:

int main ( int, char ** )
{
    LPITEMIDLIST pidl;
    HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl);


    /* Confused at this point */
    wstring wstrPath;

    wstrPath.resize ( _MAX_PATH );
    BOOL f = SHGetPathFromIDList(pidl, wstrPath.c_str () );
    /* End confusion */

The error I'm getting is:

error C2664: 'SHGetPathFromIDListW' : cannot convert parameter 2 from 'const wchar_t *' to 'LPWSTR'

Can someone help? What's the proper C++ way to do this?

Thanks!


Solution

  • The second parameter is an out parameter, so you can't just pass c_str (which is const) directly. It would probably be simplest just to do:

    wchar_t wstrPath[MAX_PATH];
    BOOL f = SHGetPathFromIDList(pidl, wstrPath);
    

    MAX_PATH is currently 260 characters.