C++:在API函数中使用std :: wstring

gui*_*ar- 3 c++ api wstring c2664

我正在使用SHGetSpecialFolderLocation API函数.我的应用程序设置为"使用Unicode字符集".

这是我到目前为止所拥有的:

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 */
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

error C2664: 'SHGetPathFromIDListW' : cannot convert parameter 2 from 'const wchar_t *' to 'LPWSTR'
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?什么是正确的C++方式来做到这一点?

谢谢!

Mat*_*hen 6

第二个参数是out参数,因此您不能直接传递c_str(即const).这可能是最简单的:

wchar_t wstrPath[MAX_PATH];
BOOL f = SHGetPathFromIDList(pidl, wstrPath);
Run Code Online (Sandbox Code Playgroud)

MAX_PATH 目前是260个字符.