Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

วิธีแปลง std::string เป็น LPCWSTR ใน C++

บทนำ

ในส่วนนี้เราจะดูวิธีแปลง C++ wide string (std::wstring) เป็น LPCWSTR (Long Pointer to Constant Wide STRing) ซึ่งเป็นสตริง wide characters โดยแปลง wide string เป็น wide character array ได้ LPCWSTR นี้เป็นของ Microsoft ต้อง include Windows.h

ใช้ฟังก์ชัน c_str() เพื่อทำให้เป็น C-style string และชี้ไปยัง wide character string

ตัวอย่างโค้ด

#include <iostream>
#include <Windows.h>
using namespace std;

int main() {
    wstring my_str = L"Hello World";
    LPCWSTR wide_string;
    wide_string = my_str.c_str();
    wcout << "my_str is : " << my_str << endl;
    wcout << "Wide String is : " << wide_string << endl;
    return 0;
}

ผลลัพธ์

my_str is : Hello World
Wide String is : Hello World