How I can use L"" prefix for wide chars

I need to use L"" to convert string to LPWSTR in path string variable

1
2
3
4
5
  //this work for me
  LPWSTR test = L"c:\\aizen.png";
  //but I need something like this
   string path="c:\\aizen.png";
   LPWSTR test = L""path; // didn't work 


thanks
Last edited on
L doesn't convert anything -- in this context it's a way of defining a wide char or string literal.

You could just use a wide string - wstring - rather than a regular (char) string - and avoid the need to convert.
http://www.cplusplus.com/reference/string/wstring/?kw=wstring

1
2
  wstring path=L"c:\\aizen.png"; // now with w- and L-
  LPWSTR test = path.c_str(); // assumes path stays in scope while you use test 

If you have a string you need to convert then the easiest approach pre-C++11 would be the C function mbstowcs()
http://www.cplusplus.com/reference/cstdlib/mbstowcs/

C++11 introduces std::codecvt_utf8_utf16, which might be more suitable.
http://www.cplusplus.com/reference/codecvt/codecvt_utf8_utf16/

Andy

PS More details:

C++ Convert string (or char*) to wstring (or wchar_t*)
http://stackoverflow.com/questions/2573834/c-convert-string-or-char-to-wstring-or-wchar-t
Last edited on
I used mbstowcs() and it work fine thanks Andy.
Last edited on
Here is my function :

1
2
3
4
5
6
7
8
9
wchar_t* convertStringToWide(string str){

    const size_t len = str.length() + 1;
    const char * x=new char[len];
    x=str.c_str();
    wchar_t * wstr;
    mbstowcs(wstr,x,len);
    return (wstr);
}
Topic archived. No new replies allowed.