wstringstream 到 LPWSTR

发布于 2024-12-02 15:52:18 字数 298 浏览 1 评论 0原文

我已经使用 wstringstream 构建了一个字符串,并且需要将其分配给 LPWSTR 类型的 struct 成员。我尝试使用 my_stringstream.str().c_str() 但出现以下编译时错误:

无法从“const wchar_t *”转换为“LPWSTR”

我该怎么做?当我尝试在 GUI 中显示字符串时,我尝试了许多不同的转换组合,其中有更多编译时错误或随机术语。

I have built up a string using wstringstream and need to assign it to a member of a struct of type LPWSTR. I try to use my_stringstream.str().c_str() but get the following compile time error:

cannot convert from 'const wchar_t *' to 'LPWSTR'

How can I do this? I have tried many different combinations of casts with either more compile time errors or random jargon when I try and display the string in a GUI.

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

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

发布评论

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

评论(6

冰雪之触 2024-12-09 15:52:18

您的函数需要一个指向可修改数据的指针,即 wchar_t*,但标准字符串类仅公开一个指向常量的指针。假设您的函数实际上可能会写入内存,我们需要为其提供一个有效的指针。

与往常一样,获取可修改缓冲区的一个简单方法是使用向量

std::vector<wchar_t> buf(mystring.begin(), mystring.end());
buf.push_back(0);                 // because your consumer expects null-termination

crazy_function(buf.data());
crazy_function(&buf[0]);          // old-style

// need a string again?
std::wstring newstr(buf.data());  // or &buf[0]

Your function expects a pointer to modifiable data, i.e. wchar_t*, but the standard string class only exposes a pointer to constant. Assuming that your function may actually write to the memory, we need to provide it with a valid pointer.

An easy way to obtain a modifiable buffer is, as always, a vector:

std::vector<wchar_t> buf(mystring.begin(), mystring.end());
buf.push_back(0);                 // because your consumer expects null-termination

crazy_function(buf.data());
crazy_function(&buf[0]);          // old-style

// need a string again?
std::wstring newstr(buf.data());  // or &buf[0]
后知后觉 2024-12-09 15:52:18

LPWSTRtypedefwchar_t*。您正在尝试将 const wchar_t* 转换为 wchar_t*。你不能隐式地这样做。

您可以使用 const_cast 来解决这个问题,但前提是您确定该函数不会修改内存:

wstring str = my_stringstream.str();
LPWSTR str = const_cast<LPWSTR>(str.c_str());

请注意,您不想这样做 const_cast< ;LPWSTR>(my_stringstream.str().c_str()) (除非您将其传递给函数),因为这将创建一个临时字符串对象,获取它的指针,将其转换为LPWSTR 然后从 str() 获得的临时字符串将在该行末尾被销毁,使您的 LPWSTR 指向已释放的内存块。

如果您将LPWSTR传递给的函数正在修改字符串,请参阅Kerrek 的回答

LPWSTR is typedefd as wchar_t*. You're trying to convert a const wchar_t* to a wchar_t*. You can't do that implicitly.

You can get around this by using const_cast, but only if you are certain the function won't modify the memory:

wstring str = my_stringstream.str();
LPWSTR str = const_cast<LPWSTR>(str.c_str());

Note that you do not want to do const_cast<LPWSTR>(my_stringstream.str().c_str()) (unless you are passing that to a function) because that will create a temporary string object, get it's pointer, convert it to a LPWSTR and then the temporary string you get from str() will be destroyed at the end of that line, leaving your LPWSTR pointing to a deallocated block of memory.

If the function you are passing the LPWSTR to is modifying the string, see Kerrek's answer.

苏辞 2024-12-09 15:52:18

如果您绝对确定字符串的内容不会被修改,则可以通过 static_castconst 强制转换;例如,如果您使用某些struct 向函数提供数据,但相同的struct 也用于检索它,则这是可以接受的情况成员是 LPWSTR 而不仅仅是 LPCWSTR

相反,如果您要向其传递 struct 的函数需要修改字符串,则有两种选择。

最安全的方法是分配字符串的单独副本作为 WCHAR 的原始动态数组,并将 wstring 的内容复制到其中。您可能希望用智能指针包装 new 的结果,除非您要转移字符串的所有权(在这种情况下,您可能必须使用一些特殊的分配函数) 。

您还可以使用 &YourString[0] 将指针传递给字符串的内部缓冲区,但是 (1) 我不确定它是否能按标准工作,(2) 它仅当该函数不会更改字符串的长度(在字符串末尾添加 L'\0')时才可以正常工作;在这种情况下,您还应该重新调整字符串的实际长度。

在最后一种情况和第一种情况下,您都必须确保您传递 struct 的函数不会期望指向的缓冲区的生存时间比您的 wstring 的范围长。 code> (小心:mystream.str() 是一个临时变量,它在您使用它的那一行就消失了,您必须将它分配给一个新变量以赋予它更广泛的范围)。

If you are absolutely sure that the content of the string will not be modified, you can cast the const away via a static_cast; a situation where this can be acceptable for example is if you are using some struct to provide data to a function, but the same struct is also used for retrieving it, so that member is LPWSTR instead of just an LPCWSTR.

If, on the contrary, the function to which you'll pass the struct needs to modify the string, you have two alternatives.

The safest one is to allocate a separate copy of the string as a raw dynamic array of WCHAR and copy there the content of the wstring. You will probably want to wrap the result of the new with a smart pointer, unless you're transferring the ownership of the string (and in that case you'll probably have to use some special allocation function).

You can also pass a pointer to the internal buffer of the string using &YourString[0], but (1) I'm not sure it's guaranteed to work by the standard, and (2) it works fine only if the function won't change the length of your string adding a L'\0' before the end of it; in this case you should also re-adjust the actual length of the string.

Both in the last and in the first case you must be sure that the function you're passing the struct to do not expect the pointed buffer to live longer than the scope of your wstring (careful: mystream.str() is a temporary that dies on the very line you use it, you have to assign it to a new variable to give it a broader scope).

放手` 2024-12-09 15:52:18
  wstringstream b;
    ..
    wchar_t z[100];
    b.read(z,100);

其中已知字符串长度小于 101。
它可以在没有 unsetf(std::ios_base::skipws) 和所有这些的情况下工作。并且 wchar_t 数组上没有 ZeroMemory

  wstringstream b;
    ..
    wchar_t z[100];
    b.read(z,100);

where string length known to be less then 101.
it works without unsetf(std::ios_base::skipws) and all this. And without ZeroMemory on wchar_t array.

羁绊已千年 2024-12-09 15:52:18

原因很简单:LPWSTR 扩展为 wchar_t *。由于指向流内容的指针是 const,因此无法将其转换为 const,除非使用 const_cast(my_stringstream.str()。 c_str())。不过,我建议反对这一点(因为你可能只是把它搞砸和/或以这种方式修改不同的东西。只有当你确定它不会被修改或者修改无关紧要时才这样做。

最简单的(和最安全的解决方案)是在缓冲区中创建您自己的由 wstringstream 提供的字符串副本,并在结构中引用该字符串,只是不要忘记稍后释放内存。

The reason for this is rather simple: LPWSTR expands to wchar_t *. As the pointer to the stream contents is a const, it's not possible to cast this const away, unless using const_cast<LPWSTR>(my_stringstream.str().c_str()). However I'd advice against this (as you might simply screw this up and/or modify something different that way. Only do it if you're sure it won't be modified or the modification won't matter.

The easiest (and most secure solution) is to create your own copy of the string provided by the wstringstream in a buffer and refer to this one in the struct. Just don't forget to free the memory later on.

海的爱人是光 2024-12-09 15:52:18
std::wstring temp(my_stringstream.str());
lpwstrVar = &temp[0];
std::wstring temp(my_stringstream.str());
lpwstrVar = &temp[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文