如何在C ++中将std :: string转换为LPCWSTR?

在本节中,我们将看到如何将C ++宽字符串(std::wstring)转换为LPCWSTR。LPCWSTR是(指向恒定宽度STRing的长指针)。它基本上是带有宽字符的字符串。因此,通过将宽字符串转换为宽字符数组,我们可以获得LPCWSTR。该LPCWSTR是Microsoft定义的。因此,要使用它们,我们必须在程序中包含Windows.h头文件。

要将std::wstring转换为宽字符数组类型的字符串,我们可以使用名为c_str()的函数将其变为类似于字符串的C并指向宽字符串。

范例程式码

#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;
}

输出结果

my_str is : Hello World
Wide String is : Hello World