Error using char* in UNICODE project

alexatsearidge used Ask the Experts™
Hi all, coding C++ in VS.NET 2003

I'm modifying a project that requires me to write all my strings as _T("string").  I know it is because of UNICODE.  The following two lines of code do not compile and I'm sure its because of UNICODE something or other.

szBuffer=new char[ myCString.GetLength()+10 ];
strcpy( szBuffer, myCString ); <--- " Cannot convert CString to const char* "
CPaintDC dc(this);
dc.DrawText( szBuffer, intMyCount, rect, MYOPTIONS ); <--- Can't convert param 1 from char* to LPCTSTR
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Try wcstrcpy and wchar_t instead of strcpy and char.
Top Expert 2012

Just use a conversion instead, e.g.

szBuffer=new char[ myCString.GetLength()+10 ];
wsprintfA( szBuffer, "%S", myCString );

The capital '%S' in the format string will convert the UNICODE string to an ANSI string.
Freelance programmer / Consultant
use TCHAR so it compiles both as ANSI and UNICODE

TCHAR* szBuffer = new TCHAR[myCString.GetLength()+10];
_tcscpy(szBuffer, myCString);

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial