In modern C ++ code, I would simply use a reliable string class like std::string , or better std::wstring to support Unicode UTF-16 with Windows. You can then convert to raw C strings at the borders of the API.
To get the read-only raw C string pointer, you can use the std::[w]string::c_str() .
Instead, if you want to change the std::[w]string buffer, you can first call the .resize() method to prepare the string buffer, which makes it large enough; then you can use &str[0] to access the internal buffer for these APIs than you need to write to it.
Note that in specific Windows ATL code, CString is another convenient string class that you might want to use. It has some platform-friendly tools, such as loading a string from resources (which is good for localizing applications).
Since CString offers implicit conversion to LPCTSTR , you can simply pass CString instances to APIs that expect raw LPCTSTR . Instead, if you need to modify the internal CString buffer, you can use the GetBuffer() / ReleaseBuffer() methods.
source share