Wednesday, May 11, 2011

C++ - char*, CString, and LPCTSTR

I have found myself looking this up several times lately because I keep forgetting. I often need to convert to or from a CString and it is often something simple.

  • For converting from a char* to a CString:

char* charStar= NULL;
// Code that sets the value of charStar
CString data;
data.Format("%s", charStar);

  • Same thing works for converting from a LPCTSTR to a CString:

void SomeMethod(LPCTSTR argument)
{
   CString data;
   data.Format("%s", argument);
}

  • And for converting from a char* to a LPCTSTR:

void SomeMethod(LPCTSTR argument)
{
}
char* charStar = NULL;
// Code that sets the value of charStar
SomeMethod(charStar);

Simple, and yet for some reason, I have had a hard time remembering this.

No comments:

Post a Comment