c++mfccharconverterslpcstr

C++ LPCTSTR to char*


I am using visual studio 2010 MFC to build a C++ program. My program calls a DLL that is not apart of the project and it accepts a char*. I have a function that gets a string in a format of LPCTSTR. I have been on google for about two hours now, and no solution found. How do I convert form a MFC LPCTSTR to a char*. Everything I have found either does not work, or just does not compile.


Solution

  • In MFC the easiest is to convert through CStringA (provided that resulting buffer will be a read-only argument):

    LPCTSTR pszA = ...
    CStringA sB(pszA);
    const char* pszC = sB;
    char* pszD = const_cast<char*>(pszC);
    

    Other options are available and were discussed: