7

我得到一个编译错误就行了:

 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

我不知道如何解决这个错误,我尝试了以下方法:

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"));

我正在使用“使用多字节字符集”设置,我不想更改它。

4

4 回答 4

5

最简单的方法是简单地使用MessageBoxW而不是MessageBox.

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

第二种最简单的方法是从原始 CString 创建一个新的 CString;它会根据需要自动转换为宽字符串和 MBCS 字符串。

CString msg = e.getAllExceptionStr().c_str();
MessageBox(msg, _T("Error initializing the sound player"));
于 2015-03-09T16:18:38.240 回答
1

LPCSTR = const char*. 您正在传递它 a const wchar*,这显然不是一回事。

始终检查您是否向 API 函数传递了正确的参数。_T("")type C-string 是宽字符串,不能与那个版本的MessageBox().

于 2015-03-09T16:19:42.743 回答
1

e.getAllExceptionStr().c_str()返回宽字符串一样,以下内容将起作用:

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

注意;W末尾的MessageBoxW

于 2015-03-09T16:19:49.383 回答
0

如果您想在过时的 MBCS 模式下编译,您可能需要使用ATL/MFC 字符串转换助手CW2T例如:

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

似乎您的getAllExceptionStr()方法返回 a std::wstring,因此调用.c_str()它返回 a const wchar_t*

CW2Twchar_t-string 转换为TCHAR-string,在您的情况下(考虑到 MBCS 编译模式),相当于char-string。

但是请注意,从 Unicode ( wchar_t-strings) 到 MBCS ( char-strings) 的转换可能是有损的。

于 2015-03-09T16:19:33.813 回答