I found a very old post, which gives the answer as:
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.
I think I understand the first part - create a new array of wchar_t and copy the contents of the LPCWSTR to it.
wstring wstrSrc(myString.begin(), myString.end());
const int lngth = sizeof(wstrSrc) / sizeof(int);
PCWSTR str2PCWSTR = wstrSrc.c_str();
const wchar_t* filepath[lngth]{ str2PCWSTR };
However, I am not sure how to do the second part - use it in the function taking a LPWSTR.
How do I use the contents of the wchar_t array in a PWSTR?
I tried searching for a clearer answer to the one I found, and tried, or made an effort to understand the suggestion.
I have not gotten any further than the code above.
LPWSTRis an alias forwchar_t*. So if you have an array ofwchar_telements, you already have aLPWSTR(remember that arrays decay to pointers to their first element).As for getting a
LPWSTRfrom awstringobject, just get a pointer to its first element:&wstrSrc[0]. That will be awchar_t*, which as said above is the same asLPWSTR. With later standards of C++ you can also usewstrSrc.data()to get the pointer.If you want a pointer to constant string (i.e.
LPCWSTR) then usewstrSrc.c_str().As for other problems
sizeofreturns the size of the object you pass to it, which in the case ofwstrSrcwill be the size of astd::wstring(assumingwstringisstd::wstring). And the size ofstd::wstringis not the length of the string, asstd::wstringonly keeps a pointer to the actual string data.To get the length in
wchar_telements from the string object use itslengthfunction:wstrSrc.length().Also note that on Windows using the MSVC compiler,
sizeof(wchar_t) != sizeof(int). On Windows using MSVC the size ofwchar_tis currently2while the size of a 32-bitintis4.