Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a copy of LPCWSTR?

Tags:

c++

c

What are the steps I have to take to duplicate a LPCWSTR string?

Consider the case: LPCWSTR str = L"Copy me";

like image 261
Mathew Kurian Avatar asked Nov 11 '13 06:11

Mathew Kurian


2 Answers

Use wcscpy(). Here is the MSDN documentation:

http://msdn.microsoft.com/en-us/library/kk6xf663(v=vs.90).aspx

A safer variant is wcscpy_s(). You have to allocate a buffer that is big enough to hold the copy up front:

   LPCWSTR str = L"Copy me";
   std::vector<wchar_t> thecopy( wcslen(str) + 1 ); // add one for null terminator
   wcscpy_s(thecopy.data(), thecopy.size(), str);

   // you can get a pointer to the copy this way:
   LPCWSTR *strCopy = thecopy.data();

wcscpy_s()'s documentation can be found here:

http://msdn.microsoft.com/en-us/library/td1esda9(v=vs.90).aspx

like image 105
Phillip Kinkade Avatar answered Sep 19 '22 03:09

Phillip Kinkade


Use wcscpy

LPWSTR wcscpy(LPWSTR szTarget, LPWCSTR szSource);

Target is non-constant wide-string (LPWSTR), and source is constant-wide-string.

LPCWSTR is defined as

typedef const WCHAR* LPCWSTR;

LP - Pointer
C - Constant
WSTR - Wide character String

like image 32
Sadique Avatar answered Sep 21 '22 03:09

Sadique