Showing posts with label string. Show all posts

How to Convert CString to string ?

Convert CString to string ...
You can get CString from many function that used in C++ and MFC, (ex: GetDlgItemText()). In some case, another function requires string type. Use can use this code

LPCTSTR Convert_CString2String (CString strVar)
{
      // Convert CString to CW2A
      CT2CA pszConvertedAnsiString (strVar);
      // Use CW2A to string
            string strResult(pszConvertedAnsiString);
     
return strResult;
}

How to Convert string to LPCTSTR ?

Convert string to LPCTSTR ...
This is a problem that I have encountered many times in coding, especially when code with the MFC. I try to find on Internet but have some way to complex for a small problem.
Finally, I and my friend found out a simple way but very effective.


LPCTSTR ConvertFromString (string strVar)
{
      // Convert string to CString
      CString tmp(strVar.c_str());
      // Use CString like LPCTSTR
      return tmp;
}