Q: How to convert a 'CString' to a 'char*'?
A: You will need this mostly to pass a 'CString' to a function that expects a 'char*'.
Note:Code:// Prototype of a function expecting a char* void func(char* c); CString csMyString = "Hello World"; // now call func() char* str = csMyString.GetBuffer(csMyString.GetLength()); func(str); // or directly func(csMyString.GetBuffer(csMyString.GetLength())); // if 'func()' modifies the passed char*, you must call csMyString.ReleaseBuffer(-1);
- 'CString::GetBuffer()' will return a 'char*' only in non-UNICODE builds.
- 'CString' has an implicit operator to 'LPCTSTR'. In non-UNICODE builds, that is a 'const char*'. Do not use a cast hack like this:
Code:func((char*)((LPCSTR) csMyString)); // BAD!!!!- Do not call any other 'CString' member function on 'csMyString' between 'GetBuffer()' and 'ReleaseBuffer()'.