kanden
kanden

Reputation: 187

How to convert LPCWSTR to LPWSTR

How can I convert LPCWSTR to LPWSTR.

In one method I am getting an argument type as LPCWSTR. This parameter(LPCWSTR) has to be passed to another method of argument type LPWSTR.

Upvotes: 7

Views: 15701

Answers (3)

David Heffernan
David Heffernan

Reputation: 613562

Create a new string, copy the contents into it, and then call the function that expects a modifiable string:

LPCWSTR str = L"bar";
std::wstring tempStr(str); 
foo(&tempStr[0]);

Upvotes: 10

Jerry Coffin
Jerry Coffin

Reputation: 490693

You probably need to create a copy of the string, and pass a pointer to the copy. An LPCWSTR i a pointer to a const, which means the content can't be modified. An LPWSTR is a pointer to non-const, meaning it may modify the content, so you need to make a copy it can modify before you can use that function.

Upvotes: 0

LPCWSTR is a pointer to a const string buffer. LPWSTR is a pointer to a non-const string buffer. Just create a new array of wchar_t and copy the contents of the LPCWSTR to it and use it in the function taking a LPWSTR.

Upvotes: 8

Related Questions