Home > Article > Backend Development > How to Convert Between std::string and LPCSTR/LPWSTR?
To convert a std::string to LPCSTR (long pointer to constant string), simply call the c_str() method on the std::string object. This will return a pointer to the internal null-terminated string buffer.
<code class="cpp">std::string myString = "Hello World!"; const char* lpcstr = myString.c_str();</code>
Converting a std::string to LPWSTR (long pointer to constant wide string) requires a few more steps:
<code class="cpp">int len = MultiByteToWideChar(CP_UTF8, 0, myString.c_str(), myString.size(), NULL, 0); wstring myWstring(len, '<pre class="brush:php;toolbar:false"><code class="cpp">const wchar_t* lpwstr = myWstring.c_str();</code>'); MultiByteToWideChar(CP_UTF8, 0, myString.c_str(), myString.size(), &myWstring[0], len);
The different terms refer to pointers to strings in different contexts:
The "LP" prefix indicates that the pointer is long, but this is no longer relevant in modern Windows development.
Remember, LPWSTR and LPCWSTR are not the same. LPCWSTR is a pointer to a constant string, while LPWSTR is a pointer to a modifiable string.
The above is the detailed content of How to Convert Between std::string and LPCSTR/LPWSTR?. For more information, please follow other related articles on the PHP Chinese website!