Share via


wstring to lpwstr

Question

Thursday, October 8, 2009 12:16 PM

Hi, i'm trying to convert a unicode 'wstring' string to lpwstr to use for list view item parameter LVITEM lvi.pszText. how can you do that?

All replies (8)

Thursday, October 8, 2009 12:50 PM âś…Answered | 2 votes

Try this...

wstring _wstring = L"Hehehe";

LVITEM lvi;
lvi.pszText = &_wstring[0];

But this is dangerous. Make sure _wstring is not modified AT ALL .Microsoft MVP - Visual C++
Blog: http://nibuthomas.com Posts are provided as is without warranties or guaranties.


Thursday, October 8, 2009 12:26 PM

If I understood you correctly :

lvi.pszText = _T("your unicode wstring");


Thursday, October 8, 2009 12:34 PM

Hi Belloc, when i try do that, i get an error.

wstring _wstring;

lvi.pszText = _T(_wstring);

error C2065: 'L_wstring' : undeclared identifier


Thursday, October 8, 2009 12:38 PM

lvi.pszText = _T(_wstring.c_str());


Thursday, October 8, 2009 12:44 PM

i still getting errors )

'L_wstring' : undeclared identifier and,
left of '.c_str' must have class/struct/union

i guess c_str() works for lpcwstr.


Thursday, October 8, 2009 12:49 PM

Try this

lvi.pszText = _wstring.c_str();

if you are building for UNICODE

 


Thursday, October 8, 2009 12:55 PM

i tried it already and got an error, cannot convert from 'const wchar_t *' to 'LPWSTR'

how can i make sure if i'm building for unicode? i just checked project properties> general tab > unicode character set is defined.

if i use #define _UNICODE

I get a warning   '_UNICODE' : macro redefinition so i think it should be already defined to work in UNICODE.


Thursday, October 8, 2009 12:58 PM

seems like with &_wstring[0] it worked !