How to Convert LPWSTR To string ?



How to Convert LPWSTR To string ?
I looked but did not find.

Note: I used a compiler MinGW With NetBeans IDE .
Last edited on
Sorry but that does not work , I used a compiler MinGW With NetBeans IDE .
As already said there, use WideCharToMultiByte() then construct your std::string.
Look the code :

1
2
3
4
5
6
7
8
9
10
LPWSTR DES_folder;      
          SHGetFolderPathW(0, CSIDL_DESKTOP, 0, 0, DES_folder);
          LPCSTR lpMyString2;
          string str1= MultiByteToWideChar(CP_ACP, 0, lpMyString2, -1,DES_folder, MAX_PATH); 
          str1+="\\printscrin.lnk";
          LPCSTR lpMyString = str1.c_str();
CreateShortCut(L"C:\\Users\\win7\\Documents\\NetBeansProjects\\CppApplication_20\\printscren.exe"
                 ,lpMyString
                 ,L"my appliaction"
                 );  


This error:
main.cpp:216:91: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
c:\mingw32\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.tcc:214:5: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermissive]
Your code posted has multiple errors, it will crash at runtime even if you make it compile.
1
2
3
4
5
6
7
8
9
10
11
12
13
WCHAR* DES_folder = new WCHAR[MAX_PATH];      
          HRESULT hr = SHGetFolderPathW(0, CSIDL_DESKTOP, 0, 0, DES_folder);
          if (SUCCEEDED (hr)) {
                std::wstring str1 = DES_folder;
                str1 += L"\\printscrin.lnk";
         
CreateShortCut(L"C:\\Users\\win7\\Documents\\NetBeansProjects\\CppApplication_20\\printscren.exe"
                 , str1.c_str()
                 , L"my appliaction"
                 );
         }
delete [] DES_folder;
           


This should work.
Last edited on
Topic archived. No new replies allowed.