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!

JBentley
  • 6,099
  • 5
  • 37
  • 72
Basj
  • 41,386
  • 99
  • 383
  • 673
  • 1
    As a Unix developer, what is `LPWSTR`!?!?! Downvoter, totally inappropriate. – Alex Chamberlain Mar 14 '13 at 22:11
  • 1
    @AlexChamberlain It's a wide string type (AFAIK, Unix dev here too). Another brainless typedef from Win(cr)API. –  Mar 14 '13 at 22:13
  • `typedef wchar_t* LPWSTR, *PWSTR;` – Jacob Seleznev Mar 14 '13 at 22:14
  • 1
    Possibly one of the worst `typedef`s I have ever seen! – Alex Chamberlain Mar 14 '13 at 22:15
  • 1
    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). – Basj Mar 14 '13 at 22:17
  • @JosBas You should probably have put that in the question; [tag:c++] is particularly known for downvoting! – Alex Chamberlain Mar 14 '13 at 22:18
  • @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... –  Mar 14 '13 at 22:18
  • LPWSTR is a long pointer to a wide string, wide strings are UNICODE. – QuentinUK Mar 14 '13 at 22:19
  • 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](http://www.joelonsoftware.com/articles/Unicode.html). – Alex Chamberlain Mar 14 '13 at 22:20
  • 1
    This has nothing to do with Visual C++ so I removed that tag and added WinAPI. – JBentley Mar 14 '13 at 22:21
  • 10
    @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 ? – Basj Mar 14 '13 at 22:35

3 Answers3

24

The C++ way:

std::wstring mywstring(mystring);
std::wstring concatted_stdstr = L"hello " + mywstring + L" blah";
LPCWSTR concatted = concatted_stdstr.c_str();
ljk321
  • 16,242
  • 7
  • 48
  • 60
6

You can use StringCchCatW function

Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
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.

ietsira
  • 29
  • 4