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

发布于 2024-09-07 05:21:56 字数 636 浏览 8 评论 0原文

我正在使用 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 */

我得到的错误是:

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

有人可以帮忙吗?执行此操作的正确 C++ 方法是什么?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情话墙 2024-09-14 05:21:56

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

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

MAX_PATH 当前为 260 个字符。

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.

此岸叶落 2024-09-14 05:21:56

wstring::c_str() 返回 const wchar_t* 并且是只读LPWSTR 不是 const 类型,并且该参数是输出参数。您需要自己分配缓冲区。你可以这样做:

wchar_t buf[MAX_PATH] = {0};
BOOL f = SHGetPathFromIDList( pidl, buf );
wstring wstrPath = buf;

wstring::c_str() returns const wchar_t* and is read only. LPWSTR is not a const type, and that parameter is an out parameter. You will need to allocate the buffer yourself. You could do something like this:

wchar_t buf[MAX_PATH] = {0};
BOOL f = SHGetPathFromIDList( pidl, buf );
wstring wstrPath = buf;
鼻尖触碰 2024-09-14 05:21:56

您可以获取 basic_string 中第一个数组项的地址作为指向可写字符串数据的指针。尽管 C++ 标准不保证该内存块必须是连续的,但这在所有已知的实现中都是安全的 (使用 std::basic_string 作为连续缓冲区的代码有多糟糕)。

std::wstring path(_MAX_PATH, L'\0');
BOOL f = SHGetPathFromIDList(pidl, &path[0]);

You can get address of 1st array item in basic_string as pointer to writable string data. Although C++ standard does not guarantee that this block of memory must be continuous this is safe in all known implementations (How bad is code using std::basic_string as a contiguous buffer).

std::wstring path(_MAX_PATH, L'\0');
BOOL f = SHGetPathFromIDList(pidl, &path[0]);
帝王念 2024-09-14 05:21:56

std::basic_string::c_str() 返回一个常量缓冲区到它的内存中。如果你想修改字符串,你必须这样做:

wstring wstrPath;
wstrPath.resize( MAX_PATH );
BOOL f = SHGetPathFromIDList(pidl, &wstrPath[0]);
wstrPath.erase(
   std::find(wstrPath.begin(), wstrPath.end(), L'\0'), wstrPath.end()
); //Throw away unused buffer space

编辑:如果你不害怕 C 库,这个应该也可以工作(尽管我没有像我一样测试过它)已经测试了上面的实现):

wstring wstrPath;
wstrPath.resize( MAX_PATH );
BOOL f = SHGetPathFromIDList(pidl, &wstrPath[0]);
wstrPath.resize(wcslen(wstrPath.c_str()));

std::basic_string::c_str() returns a constant buffer to it's memory. If you want to modify the string, you'd have to do something like this:

wstring wstrPath;
wstrPath.resize( MAX_PATH );
BOOL f = SHGetPathFromIDList(pidl, &wstrPath[0]);
wstrPath.erase(
   std::find(wstrPath.begin(), wstrPath.end(), L'\0'), wstrPath.end()
); //Throw away unused buffer space

EDIT: This should also work if you're not afraid of C libraries (though I've not tested it like I've tested the implementation above):

wstring wstrPath;
wstrPath.resize( MAX_PATH );
BOOL f = SHGetPathFromIDList(pidl, &wstrPath[0]);
wstrPath.resize(wcslen(wstrPath.c_str()));
热鲨 2024-09-14 05:21:56

wstring::c_str() 不允许您以这种方式修改其内部缓冲区。最简单的解决方法是自己创建一个 wchar_t 缓冲区,并将其传递给 wstring 构造函数:

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

wstring::c_str() does not let you modify its internal buffer in this way. Your easiest workaround is to create a wchar_t buffer yourself, and the pass that to the wstring constructor:

wchar_t buf[MAX_PATH];
BOOL f = SHGetPathFromIDList(pidl, buf );
wstring wstrPath(buf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文