C++은 string 종류가 많고 그 변환이 다 까다로우므로 별도로 정리.

기본 흐름은 string 시리즈끼리 변환이 되고, wstring 시리즈끼리 변환이 쉽게 되는데, 만일 바로 변환이 안 되는 경우 —물론 아예 new를 해서 만드는 방법을 써도 되긴 하지만, 이 경우 delete도 해줘야 하므로 논외로 친다— 는 string이나 wstring으로 변환한 후에 원하는 타입으로 변환하는 단계를 거쳐야 한다.

string ↔ wsting

string str = "Hello World";
wstring wstr = L"Hello World";

// wstring을 string으로 변환
string str2(wstr.begin(), wstr.end());

// string을 wstring으로 변환
wstring wstr2(str.begin(), str.end());

cout << "str: " << str << endl;  // str: Hello World
cout << "wstr to str: " << str2 << endl;  // wstr to str: Hello World

// wstring을 찍을 때는 wcout을 써야 한다. 
wcout << "wstr: " << wstr << endl;  // wstr: Hello World
wcout << "str to wstr: " << wstr2 << endl;  // str to wstr: Hello World

string ↔ LPSTR, LPCSTR

string str = "Hello World";

// string을 LPSTR, LPCSTR로 변환할 때 0번째의 주소를 넘기면 된다.
LPSTR lpstr = &str[0];
LPCSTR lpcstr = &str[0];

// LPSTR, LPCSTR은 string에 그대로 대입 가능하다.
string str2 = lpstr;
string str3 = lpcstr;

// LPSTR은 LPCSTR에 그대로 대입 가능하다.
LPCSTR lpcstr2 = lpstr;

// error! LPCSTR은 LPSTR에 대입 불가능하다. 
// 이 경우에는 LPSTR을 string으로 변환한 후에 LPCSTR로 변환해야 한다.
// LPSTR lpstr2 = lpcstr;  // error!

cout << "str to lpstr: " << lpstr << endl;  // str to lpstr: Hello World
cout << "str to lpcstr: " << lpcstr << endl;  // str to lpcstr: Hello World
cout << "lpstr to str: " << str2 << endl;  // lpstr to str: Hello World
cout << "lpcstr to str: " << str3 << endl;  // lpcstr to str: Hello World
cout << "lpstr to lpcstr: " << lpcstr2 << endl;  // lpcstr to lpcstr: Hello World

wstring ↔ LPWSTR, LPCWSTR

wstring wstr = L"Hello World";

// wstring을 LPWSTR, LPCWSTR로 변환할 때 0번째의 주소를 넘기면 된다.
LPWSTR lpwstr = &wstr[0];
LPCWSTR lpcwstr = &wstr[0];

// LPWSTR, LPCWSTR은 wstring에 그대로 대입 가능하다.
wstring wstr2 = lpwstr;
wstring wstr3 = lpcwstr;

// LPWSTR은 LPCWSTR에 그대로 대입 가능하다.
LPCWSTR lpcwstr2 = lpwstr;

// error! LPCWSTR은 LPWSTR에 대입 불가능하다. 
// 이 경우에는 LPWSTR을 wstring으로 변환한 후에 LPCWSTR로 변환해야 한다.
// LPWSTR lpwstr2 = lpcwstr;  

wcout << "wstr to lpwstr: " << lpwstr << endl;  // wstr to lpwstr: Hello World
wcout << "wstr to lpcwstr: " << lpcwstr << endl;  // wstr to lpcwstr: Hello World
wcout << "lpwstr to wstr: " << wstr2 << endl;  // lpwstr to wstr: Hello World
wcout << "lpwcstr to wstr: " << wstr3 << endl;  // lpcwstr to wstr: Hello World
wcout << "lpwstr to lpcwstr: " << lpcwstr2 << endl;  // lpcwstr to lpcwstr: Hello World