Basj
Basj

Reputation: 46371

Concatenation of LPWSTR strings

In Visual C++, I have a

LPWSTR mystring;

which is already defined somewhere else in the code.

I want to create a new LPWSTR containing:

"hello " + mystring + " blablabla"        (i.e. a concatenation)

I'm getting mad with such a simple thing (concatenation)! Thanks a lot in advance, I'm lost!

Upvotes: 16

Views: 18210

Answers (3)

ietsira
ietsira

Reputation: 29

std::wstring mystring_w(mystring);
std::wstring out_w = L"hello " + mystring_w + L" blablabla";
LPWSTR out = const_cast<LPWSTR>(out_w.c_str());

'out' is a LPWSTR wrapper for 'out_w'. So as long as 'out_w' is in scope it will be good to use. Also you don't need to delete 'out' as it's is binded to 'out_w' lifecycle.

This is pretty much the same answer 'user529758' gave, but with 'chris' proposed modification.

Upvotes: 0

user529758
user529758

Reputation:

The C++ way:

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

Upvotes: 24

Jacob Seleznev
Jacob Seleznev

Reputation: 8131

You can use StringCchCatW function

Upvotes: 6

Related Questions