none
wstring to lpwstr RRS feed

  • Question

  • 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?
    Thursday, October 8, 2009 12:16 PM

Answers

All replies

  • If I understood you correctly :

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

    Thursday, October 8, 2009 12:26 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:34 PM
  • lvi.pszText = _T(_wstring.c_str());
    • Edited by Belloc Thursday, October 8, 2009 12:40 PM correction
    Thursday, October 8, 2009 12:38 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:44 PM
  • Try this

    lvi.pszText = _wstring.c_str();

    if you are building for UNICODE

     

    Thursday, October 8, 2009 12:49 PM
  • 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.
    • Proposed as answer by Belloc Thursday, October 8, 2009 1:01 PM
    • Marked as answer by Parcxs Thursday, October 8, 2009 1:16 PM
    Thursday, October 8, 2009 12:50 PM
    Moderator
  • 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:55 PM
  • seems like with &_wstring[0] it worked !
    Thursday, October 8, 2009 12:58 PM