Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ convert from LPCTSTR to const char *

I have this problem in MSVC2008 MFC. I´m using unicode. I have a function prototype:

MyFunction(const char *)

and I'm calling it:

MyfunFunction(LPCTSTR wChar). 

error:Cannot Convert Parameter 1 From 'LPCTSTR' to 'const char *'

How to resolve it?

like image 200
Annie Avatar asked Jul 05 '11 19:07

Annie


1 Answers

Since you're using MFC, you can easily let CString do an automatic conversion from char to TCHAR:

MyFunction(CString(wChar));

This works whether your original string is char or wchar_t based.

Edit: It seems my original answer was opposite of what you asked for. Easily fixed:

MyFunction(CStringA(wChar));

CStringA is a version of CString that specifically contains char characters, not TCHAR. There's also a CStringW which holds wchar_t.

like image 55
Mark Ransom Avatar answered Oct 10 '22 09:10

Mark Ransom