Loading...

How to convert std::string to TCHAR*

반응형

How to convert std::string to TCHAR*

Most of the time people used to get this error , cannot convert std::string to TCHAR*
Here is the code snippet which will convert std::string to TCHAR* and vice verse

 
typedef std::basic_string<TCHAR> tstring;  
 
TCHAR* StringToTCHAR(string& s)
{
  tstring tstr;
  const char* all = s.c_str();
  int len = 1 + strlen(all);
  wchar_t* t = new wchar_t[len]; 
  if (NULL == t) throw std::bad_alloc();
  mbstowcs(t, all, len);
  return (TCHAR*)t;
}
 
std::string TCHARToString(const TCHAR* ptsz)
{
     int len = wcslen((wchar_t*)ptsz);
     char* psz = new char[2*len + 1];
     wcstombs(psz, (wchar_t*)ptsz, 2*len + 1);
     std::string s = psz;
     delete [] psz;
     return s;
}

link : http://ukanth.in/blog/?p=180

'Study > C++' 카테고리의 다른 글

메모리맵 파일  (0) 2011.04.09
LIB / DLL 차이점  (0) 2011.03.17
문자열 비교할때 대소문자 구분없이 하기.  (0) 2010.12.03
dllimport, dllexport  (0) 2010.11.15
stl을 DLL제작시 문제점..warning C4251: 에 대한 대처  (0) 2010.11.10
TAGS.

Comments