Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' in MFC / C++ project

I get a compilation error on the line:

 MessageBox(e.getAllExceptionStr().c_str(), _T("Error initializing the sound player"));

Error   4   error C2664: 'CWnd::MessageBoxA' : cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR'   c:\users\daniel\documents\visual studio 2012\projects\mytest1\mytest1\main1.cpp 141 1   MyTest1

I don't know how to resolve this error, I tried the following:

MessageBox((wchar_t *)(e.getAllExceptionStr().c_str()), _T("Error initializing the sound player"));
MessageBox(_T(e.getAllExceptionStr().c_str()), _T("Error initializing the sound player"));

I am using the setting "Use Multi-Byte Character Set" and I don't want to change it.

like image 988
gornvix Avatar asked Mar 09 '15 16:03

gornvix


3 Answers

The easiest way is simply to use MessageBoxW instead of MessageBox.

MessageBoxW(e.getAllExceptionStr().c_str(), L"Error initializing the sound player");

The second easiest way is to create a new CString from the original; it will automatically convert to/from wide string and MBCS string as necessary.

CString msg = e.getAllExceptionStr().c_str();
MessageBox(msg, _T("Error initializing the sound player"));
like image 185
Mark Ransom Avatar answered Oct 09 '22 15:10

Mark Ransom


LPCSTR = const char*. You are passing it a const wchar*, which clearly is not the same thing.

Always check that you are passing API functions the right parameters. _T("") type C-string are wide strings and can't be used with that version of MessageBox().

like image 1
dandan78 Avatar answered Oct 09 '22 14:10

dandan78


As e.getAllExceptionStr().c_str() is returning wide string then the following will work:

MessageBoxW(e.getAllExceptionStr().c_str(), L"Error initializing the sound player");

Note the W on the end of MessageBoxW;

like image 1
Richard Critten Avatar answered Oct 09 '22 16:10

Richard Critten