How to convert wstring to LPOLESTR?

Naveen picture Naveen · Jul 13, 2011 · Viewed 8.7k times · Source

The below is the one that I have tried and it did not work.

std::wstring = L"Text";
USES_CONVERSION;
LPOLESTR lpDesc = W2OLE((LPWSTR)wsDescr.c_str());

Please any one cany say what is the better way to do?

Answer

user195488 picture user195488 · Jul 13, 2011

LPOLESTR is a string of OLECHAR which is essentially wchar_t. So LPOLESTR is a null-terminated wchar_t*. LPOLESTR is a typedef created by Microsoft. These are vestiges of an automatic ANSI / Unicode conversion scheme that Microsoft used prior to MFC 4.0 and has since abandoned. For Win32 development, "OLE" corresponds to Unicode. For example, in Win32 development, an OLECHAR is simply a wchar_t and an LPOLESTR is a wide character string (e.g. wchar_t*).

To construct wstring from an array of wchar_t characters it is straight forward -

wchar_t* Array = L"Hello";
std::wstring strArray(Array);

to convert the other direction from wstring to wchar_t*, you can do this -

wstring wstr = L"Test wstring";
const wchar_t *pwstr = wstr.c_str();

you can also try this,

LPOLESTR tempString = W2OLE((wchar_t*)wstring_temp.c_str());