Concatenation of LPWSTR strings
Asked Answered
E

3

16

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!

Eliseoelish answered 14/3, 2013 at 22:8 Comment(11)
As a Unix developer, what is LPWSTR!?!?! Downvoter, totally inappropriate.Jase
@AlexChamberlain It's a wide string type (AFAIK, Unix dev here too). Another brainless typedef from Win(cr)API.Chiang
typedef wchar_t* LPWSTR, *PWSTR;Terrazas
Possibly one of the worst typedefs I have ever seen!Jase
I googled this thirty times already... of course! But I'm still unable to do this simple task... It's not as simple as it seems : because of LPWSTR, it's not very easy (for me).Eliseoelish
@JosBas You should probably have put that in the question; c++ is particularly known for downvoting!Jase
@AlexChamberlain What I especially hate about the Windows API (apart from the Windows API) is that they're abusing C naming conventions. What the heck is LPWSTR if not a preprocessor macro? Oh wait, no, it's a no-brainer typedef... Well played, Microsoft, don't even leave a small hole for obeying standards...Chiang
LPWSTR is a long pointer to a wide string, wide strings are UNICODE.Threnode
Wide strings aren't necessarily Unicode, that is a common misconception; in fact, they may not be wide enough depending on how you defined Unicode... See Joel's Infamous Post.Jase
This has nothing to do with Visual C++ so I removed that tag and added WinAPI.Isolda
@ShmilTheCat : why did you loose 1 minute of your life to say to me "one minute Google on ..." : do you really think I haven't done it yet ?Eliseoelish
C
24

The C++ way:

std::wstring mywstring(mystring);
std::wstring concatted_stdstr = L"hello " + mywstring + L" blah";
LPCWSTR concatted = concatted_stdstr.c_str();
Chiang answered 14/3, 2013 at 22:12 Comment(1)
One modification would be that concatted must be a LPCWSTR.Bilinear
T
6

You can use StringCchCatW function

Terrazas answered 14/3, 2013 at 22:12 Comment(7)
That's not C++, or certainly shouldn't be.Jase
@Captain He's already dealing with LPWSTR, so why not? This way, he avoids converting his desired type to a different type just to manipulate it, before converting it back again. I'd +1 if it weren't for "You need" (which implies no other solution).Isolda
@AlexChamberlain The question is about Visual C++.Terrazas
@Isolda Changed need to to canTerrazas
@JacobSeleznev Totally irrelevant; it is not modern C++.Jase
@Alex LPWSTR is not modern C++, so we're already outside that domain. Why therefore limit the solution to modern C++ if a practical solution exists within the problem's domain? As I said above, he ultimately needs to convert his result back to LPWSTR anyway.Isolda
@Isolda Because it then becomes a question of security and clarity.Jase
S
0
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.

Sestos answered 20/9, 2020 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.