Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to combine two LPCWSTRs?

I have two variables that are LPCWSTRs. I want to create a new variable that will have the values of the first and second variable.
I tried this but it didn't work.

LPCWSTR d = L"sd";
LPCWSTR f = L"f";
LPCWSTR df = d + f;

I get this error when i try that.

1   IntelliSense: expression must have integral or enum type

Is there a function that can combine two LPCWSTRs?

like image 362
Ramilol Avatar asked Dec 20 '10 20:12

Ramilol


2 Answers

In C++ it is usually a good idea to use std::string for manipulations with strings. In your case it could look like:

LPCWSTR d = L"sd";
LPCWSTR f = L"f";
std::wstring df = std::wstring(d) + f;
LPCWSTR dfc = df.c_str(); // if you are really need this
like image 65
Kirill V. Lyadvinsky Avatar answered Oct 07 '22 18:10

Kirill V. Lyadvinsky


It doesn't work because the C++ compiler cannot generate code to join together arrays. The two strings in the example are arrays of type wchar_t. To join arrays you must use higher level functions. There are several ways of doing it:

LPWSTR df[20]; // cannot be LPCWSTR, because the C is for const.
wcsprintf(df, L"%s%s", d, f);

or

LPWSTR df[20];
wcscpy(df, d);
wcscat(df, f);

or use STL as previously answered.

like image 28
Michael Smith Avatar answered Oct 07 '22 18:10

Michael Smith