简体   繁体   中英

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 .

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.

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]);

You probably need to create a copy of the string, and pass a pointer to the copy. An LPCWSTR ia 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM