Ok, so here is what I currently have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
class CStringBuffer : public std::wstringbuf
{
public:
CStringBuffer()
: std::wstringbuf()
{ }
virtual ~CStringBuffer()
{ }
public:
//Operators.
operator LPWSTR()
{
wchar_t *ptr = this->gptr();
return this->gptr();
}
};
|
Basically, I just need a string buffer because I am working with the Windows API
MultiByteToWideChar(), and I want the buffer managed. So my first thought was: Use a string stream (well, a
wostringstream) and that's it. Well, it seems that I could not get access to the underlying buffer.
Reading a bit, I discovered that the string stream uses an internal string buffer, and that it seems quite possible to inherit from the default class, in this case
std::wstringbuf. So good. I went ahead and created the class you see above, and then set the buffer in the string stream using
set_rdbuf(). The operator found in the class provides the raw pointer and all is good: I can call
MultiByteToWideChar() and I can see how the memory contents change while debugging in a memory window. Nice.
But by the time I call
str() on the string stream I get a blank string. Why?
I also accept easier ways of getting the job done, which is: Convert a multi byte string encoded using the default Windows encoding to Unicode.