반응형
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
반응형
'IT' 카테고리의 다른 글
Guid에 대한 C # 정규식 (0) | 2020.08.03 |
---|---|
각도 2 드롭 다운 옵션 (0) | 2020.08.03 |
맞춤 HTML5 필수 항목 확인 메시지 설정 (0) | 2020.08.03 |
LESS의 빠른 인 하위 선택기 (0) | 2020.08.03 |
시간을 변경하지 않고 C # DateTime을 UTC 시간으로 (0) | 2020.08.03 |