를로 어떻게 변환 할 std::string수 LPCSTR있습니까? 또한 어떻게을로 변환 할 std::string수 LPWSTR있습니까?
나는 이것들 LPCSTR LPSTR LPWSTR과 완전히 혼란 스럽습니다 LPCWSTR.
인가 LPWSTR와 LPCWSTR같은?
답변
str.c_str()당신을 제공 const char *인 LPCSTR(상수 문자열 긴 포인터) – 그것은에 대한 포인터 것을 의미 0문자로 끝나는 문자열. 대신 W넓은 문자열을 의미합니다 .wchar_tchar
답변
c_str()에서 const char *( LPCSTR) 를 가져 오려면 전화 하세요 std::string.
모든 것이 이름에 있습니다.
LPSTR -(긴) 문자열에 대한 포인터- char *
LPCSTR -(긴) 상수 문자열에 대한 포인터- const char *
LPWSTR -(긴) 유니 코드 (와이드) 문자열에 대한 포인터- wchar_t *
LPCWSTR -상수 유니 코드 (와이드) 문자열에 대한 (긴) 포인터- const wchar_t *
LPTSTR -(긴) TCHAR에 대한 포인터 (UNICODE가 정의 된 경우 유니 코드, 그렇지 않은 경우 ANSI) 문자열- TCHAR *
LPCTSTR -(긴) 상수 TCHAR 문자열에 대한 포인터- const TCHAR *
이름의 L (긴) 부분은 무시할 수 있습니다. 이는 16 비트 Windows에서 유지 된 것입니다.
답변
이들은 다음에 해당하는 Microsoft 정의 typedef입니다.
LPCSTR : null로 끝나는 const 문자열에 대한 포인터 char
LPSTR : null로 끝나는 char 문자열에 대한 포인터 char(종종 버퍼가 전달되어 ‘출력’매개 변수로 사용됨)
LPCWSTR : null로 끝나는 const 문자열에 대한 포인터 wchar_t
LPWSTR : null로 끝나는 문자열에 대한 포인터 wchar_t(종종 버퍼가 전달되어 ‘출력’매개 변수로 사용됨)
a std::string를 LPCSTR 로 “변환”하려면 정확한 컨텍스트에 따라 다르지만 일반적으로 호출 .c_str()만으로 충분합니다.
작동합니다.
void TakesString(LPCSTR param);
void f(const std::string& param)
{
TakesString(param.c_str());
}
이와 같은 작업을해서는 안됩니다.
LPCSTR GetString()
{
std::string tmp("temporary");
return tmp.c_str();
}
에서 반환 된 버퍼 .c_str()는 std::string인스턴스 가 소유 하며 문자열이 다음에 수정되거나 삭제 될 때까지만 유효합니다.
a std::string를 a 로 변환하는 LPWSTR것은 더 복잡합니다. 을 원하는 것은 LPWSTR당신이 수정 버퍼를 필요로하고 당신은 또한 당신이 이해하는지 확인해야한다는 것을 의미 인코딩 문자std::string 사용하고 있습니다. 에 std::string시스템 기본 인코딩을 사용하는 문자열 이 포함되어있는 경우 (여기서는 windows 가정) 필요한 와이드 문자 버퍼의 길이를 찾고 MultiByteToWideChar(Win32 API 함수)를 사용하여 코드 변환을 수행 할 수 있습니다.
예 :
void f(const std:string& instr)
{
// Assumes std::string is encoded in the current Windows ANSI codepage
int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
if (bufferlen == 0)
{
// Something went wrong. Perhaps, check GetLastError() and log.
return;
}
// Allocate new LPWSTR - must deallocate it later
LPWSTR widestr = new WCHAR[bufferlen + 1];
::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
// Ensure wide string is null terminated
widestr[bufferlen] = 0;
// Do something with widestr
delete[] widestr;
}
답변
사용 LPWSTR하면 가리키는 문자열의 내용을 변경할 수 있습니다. 사용 LPCWSTR하면 가리키는 문자열의 내용을 변경할 수 없습니다.
std::string s = SOME_STRING;
// get temporary LPSTR (not really safe)
LPSTR pst = &s[0];
// get temporary LPCSTR (pretty safe)
LPCSTR pcstr = s.c_str();
// convert to std::wstring
std::wstring ws;
ws.assign( s.begin(), s.end() );
// get temporary LPWSTR (not really safe)
LPWSTR pwst = &ws[0];
// get temporary LPCWSTR (pretty safe)
LPCWSTR pcwstr = ws.c_str();
LPWSTR원래 문자열에 대한 포인터 일뿐입니다. 위의 샘플을 사용하여 함수에서 반환하면 안됩니다. 일시적이지 않게하려면 LPWSTR힙에 원래 문자열의 복사본을 만들어야합니다. 아래 샘플을 확인하십시오.
LPWSTR ConvertToLPWSTR( const std::string& s )
{
LPWSTR ws = new wchar_t[s.size()+1]; // +1 for zero at the end
copy( s.begin(), s.end(), ws );
ws[s.size()] = 0; // zero at the end
return ws;
}
void f()
{
std::string s = SOME_STRING;
LPWSTR ws = ConvertToLPWSTR( s );
// some actions
delete[] ws; // caller responsible for deletion
}
답변
MultiByteToWideChar찰스 베일리가 한 대답 은 정답입니다. 에 LPCWSTR대한 typedef const WCHAR*일 뿐이 므로 widestr예제 코드에서는 a LPWSTR가 예상 되는 곳 이나 a 가 예상 되는 곳마다 사용할 수 있습니다 LPCWSTR.
한 가지 사소한 조정은 std::vector<WCHAR>수동으로 관리되는 배열 대신 사용하는 것입니다.
// using vector, buffer is deallocated when function ends
std::vector<WCHAR> widestr(bufferlen + 1);
::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), &widestr[0], bufferlen);
// Ensure wide string is null terminated
widestr[bufferlen] = 0;
// no need to delete; handled by vector
또한 시작하기 위해 넓은 문자열로 작업해야하는 경우 std::wstring대신을 사용할 수 있습니다 std::string. Windows TCHAR유형 으로 작업 하려면 std::basic_string<TCHAR>. 에서 std::wstring로 LPCWSTR또는에서 std::basic_string<TCHAR>로 변환 LPCTSTR하는 것은 호출의 문제입니다 c_str. ANSI와 UTF-16 문자 사이를 변경할 때 MultiByteToWideChar(및 그 반대 WideCharToMultiByte) 그림이 나타납니다.
답변
변환은 간단합니다.
std :: string str; LPCSTR lpcstr = str.c_str ();
답변
변환은 간단합니다.
std::string myString;
LPCSTR lpMyString = myString.c_str();
여기서주의해야 할 한 가지는 c_str이 myString의 복사본을 반환하지 않고 std :: string이 래핑하는 문자열에 대한 포인터 만 반환한다는 것입니다. 복사본을 원하거나 필요로한다면 strcpy를 사용하여 직접 만들어야합니다.