或者如何使用 wchar_t* 初始化 wstring?
我试过类似的方法,但效果不佳。 我得到了一个 LPVOID,它指向一个 wchar_t 指针。 我只是想把它变成一个 wstring,这样我就可以在上面使用一些普通的字符串函数。
LPVOID lpOutBuffer = NULL;
//later in code this is initialized this way
lpOutBuffer = new WCHAR[dwSize/sizeof(WCHAR)];
//fills up the buffer
doStuff(lpOutBuffer, &dwSize);
//try to convert it to a wstring
wchar_t* t = (wchar_t*)lpOutBuffer;
wstring responseHeaders = wstring(t);
printf("This prints response headers: \n%S", t);
printf("This prints nothing: \n%S", responseHeaders);
doStuff 实际上是对 WinHttpQueryHeaders 的调用 我只是对其进行了更改,以便更容易理解我的示例。
最佳答案
将 wstring
对象传递给 printf
是行不通的。将第二个 prinf
行改写为
printf("This prints nothing: \n%S", responseHeaders.c_str());
c_str() 为您提供指向底层字符串数据的 const
指针。
关于c++ - 如何从 wchar_t* 转换为 wstring?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8916159/