LPWSTR
is a pointer to an array of wide characters. It is not a class with a + overload, so you cannot concatenate LPWSTR
s with +. Consider using wstring
instead.
#define UNICODE
#include<windows.h>
#include<string>
int main()
{
std::wstring str1(L"vishal ");
std::wstring str2(L"nechwani");
std::wstring str3 = str1 + str2;
MessageBox(NULL, str3.c_str(), str3.c_str(), MB_OK);
return 0;
}
If you must suffer through c-style strings, use wcscat
, but do not forget to pre-allocate storage for str3
.
Edit: Doing this the Stupid Way
This is the stupid way because look at all the extra work you have to do:
#define UNICODE
#include<cwchar>
#include<windows.h>
int main()
{
LPCWSTR str1=L"vishal "; // note LPCWSTR. L"vishal " provides a constant array
// and it should be assigned to a constant pointer
LPCWSTR str2=L"nechwani";
// find out how much space we need
size_t len = wcslen(str1) + // length string 1
wcslen(str2) + // length string 2
1; // null terminator. Can't have a c-style string without one
LPWSTR str3 = new wchar_t[len]; // allocate space for concatenated string
// Note you could use a std::unique_ptr here,
// but for smurf's sake just use a wstring instead
str3[0] = L'\0'; // null terminate string
//add string 1 and string 2 to to string 3
wcscat(str3,str1);
wcscat(str3,str2);
MessageBox(NULL,str3,str3,MB_OK);
delete[] str3; // release storage allocated to str3
return 0;
}
No shame in being confused by this mess. It is a mess.
wcsncat
may not be the right tool to use here. To properly display the concatenated string you must either size the buffer so that it is too big to truncate or allocate a buffer large enough to contain the string. I've chosen to allocate a sufficiently large buffer. Also note that wcsncat
can still overrun the end of your buffer placing the null terminator, so the count parameter must be no more than one less than the size of the buffer.
wstring
does all of this crap for you and adds on a number of other useful operations free of charge. Not using a string
without a good reason to avoid string
is stupid. Don't be stupid.