Wednesday 22 July 2015

Concatenating wide character strings

Today I was looking at concatenating multiple wide character strings. I was dealing with PWSTRs. As it turns out, I needed to use wstring to do it. The following code illustrates the method.

std::wstring mywstring(mystring);
std::wstring concatted_stdstr = L"hello " + mywstring + L" blah";
LPWSTR concatenated = concatted_stdstr.c_str();

However, the above gives us a read-only value for the concatenated string. We can use wcsdup() to get a writeable copy. Otherwise, we can cast the read-only string to a writeable one as follows.

PWSTR value = const_cast (concatenated);

No comments: