How to convert std::string to LPCWSTR in C++?

C++Server Side ProgrammingProgramming

In this section we will see how to convert C++ wide string (std::wstring) to LPCWSTR. The LPCWSTR is the (Long Pointer to Constant Wide STRing). It is basically the string with wide characters. So by converting wide string to wide character array we can get LPCWSTR. This LPCWSTR is Microsoft defined. So to use them we have to include Windows.h header file into our program.

To convert std::wstring to wide character array type string, we can use the function called c_str() to make it C like string and point to wide character string.

Example Code

#include<iostream>
#include<Windows.h>
using namespace std;
main(){
   wstring my_str = L"Hello World";
   LPCWSTR wide_string ; //define an array with size of my_str + 1
   wide_string = my_str.c_str();
   wcout << "my_str is : " << my_str <<endl;
   wcout << "Wide String is : " << wide_string <<endl;
}

Output

my_str is : Hello World
Wide String is : Hello World
raja
Published on 04-Apr-2019 13:11:35
Advertisements