Gold mine of Visual C++ tricks!
Archive for December 17, 2008
How to convert CString to char* or LPTSTR?
Dec 17th
![]()
Without a second thought, I can say that it will be one of the first problems that beginners face – How to convert a CString to LPTSTR. I’ve seen this question several times in forums. Well, i think the CString to LPTSTR conversion is just like this picture.

![]()
Well, you can use CString::GetBuffer() to access the internal buffer of CString. But one thing to take care is that – you should release the buffer by calling CString::ReleaseBuffer() after use. Check the code snippet below,
// Our CString object. CString String = "HelloWorld"; // Get the internal buffer pointer of CString. LPTSTR pString = String.GetBuffer( 0 ); ... // Use the pString and then release it. String.ReleaseBuffer();
![]()
Now get rid of that nasty error message – error C2664: ‘Hello’ : cannot convert parameter 1 from ‘class CString’ to ‘char *’.
![]()
Targeted Audience – Beginners.