C++ LPCTSTR to char*

Landin Martens picture Landin Martens · Mar 20, 2012 · Viewed 15k times · Source

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.

Answer

Roman R. picture Roman R. · Mar 20, 2012

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: