IT

C ++에서 std :: string을 LPCWSTR로 변환하는 방법 (유니 코드)

lottoking 2020. 8. 3. 17:26
반응형

C ++에서 std :: string을 LPCWSTR로 변환하는 방법 (유니 코드)


std :: string을 LPCWSTR로 변환하기위한 메소드 또는 코드 스 니펫을 찾고 있습니다.


MSDN 기사 링크를 주셔서 감사합니다. 이것이 바로 내가 찾던 것입니다.

std::wstring s2ws(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

std::wstring stemp = s2ws(myString);
LPCWSTR result = stemp.c_str();

이 솔루션은 실제로 다른 제안보다 훨씬 뛰어납니다.

std::wstring stemp = std::wstring(s.begin(), s.end());
LPCWSTR sw = stemp.c_str();

어떤 플랫폼 독립적입니다. h2h :)


ATL / MFC 환경에있는 경우 ATL 변환 매크로를 사용할 수 있습니다.

#include <atlbase.h>
#include <atlconv.h>

. . .

string myStr("My string");
CA2W unicodeStr(myStr);

그런 다음 unicodeStr을 LPCWSTR로 사용할 수 있습니다. 유니 코드 프로세서의 메모리가 스택에 생성되고 해제 된 다음 unicodeStr의 소멸자가 실행됩니다.


std :: string을 사용하는 대신 std :: wstring을 사용할 수 있습니다.

편집 : 이것은 더 설명이 죄송합니다.

std :: wstring :: c_str () 사용


string  myMessage="helloworld";
int len;
int slength = (int)myMessage.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, myMessage.c_str(), slength, 0, 0); 
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, myMessage.c_str(), slength, buf, len);
std::wstring r(buf);
 std::wstring stemp = r.C_str();
LPCWSTR result = stemp.c_str();

LPCWSTR lpcwName = std :: wstring (strname.begin (), strname.end ()). c_str ()

참고 URL : https://stackoverflow.com/questions/27220/how-to-convert-stdstring-to-lpcwstr-in-c-unicode

반응형