7

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.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
gornvix
  • 3,154
  • 6
  • 35
  • 74
  • I'm not sure why `getAllExceptionStr` returns a wide string if you're stuck with ANSI, but then you'll have to *convert* (note: not cast) it. – chris Mar 09 '15 at 16:17

4 Answers4

5

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"));
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • I think if the purpose of the conversion is to just pass the string as a temporary to some function/method like `MessageBox()` in the above case, using `CW2T` for the conversion is more efficient than creating a new `CString` instance. `CString` has more features than `CW2T`, but also **more overhead**. Moreover, `CW2T` and the other ATL conversion helpers also implement an optimization for "small" strings, with a _stack-allocated buffer_ for small strings, instead of allocating memory from the heap. – Mr.C64 Mar 09 '15 at 16:44
  • @Mr.C64 I'm sure you're right, but I think worrying about overhead in a call to MessageBox could be the poster child for *premature optimization*. – Mark Ransom Mar 09 '15 at 17:39
  • Everyone is free to write the code he likes. I prefer using the aforementioned helpers when there are strings to be converted as parameters to functions/methods (including MessageBox): to me that is higher quality code than using CString in that context. Everyone has his own programming style. I think it's better to use high-quality style independently if you call foo(), bar() or MessageBox(). – Mr.C64 Mar 09 '15 at 20:19
1

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().

dandan78
  • 13,328
  • 13
  • 64
  • 78
1

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;

Richard Critten
  • 2,138
  • 3
  • 13
  • 16
0

If you want to compile in the obsolete MBCS mode, you may want to use the ATL/MFC string conversion helpers, like CW2T, e.g.:

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

It seems that your getAllExceptionStr() method returns a std::wstring, so calling .c_str() on it returns a const wchar_t*.

CW2T converts from wchar_t-string to TCHAR-string, which in your case (considering the MBCS compilation mode), is equivalent to char-string.

Note however that conversions from Unicode (wchar_t-strings) to MBCS (char-strings) can be lossy.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162