from:http://community.sgdotnet.org/forums/p/9044/26197.aspx
Let's look at the definition of AfxMessageBox.
int AfxMessageBox(
LPCTSTR lpszText,
UINT nType = MB_OK,
UINT nIDHelp = 0
);
This is what we're looking at. LPCTSTR is a LPCWSTR if unicode, or LPCSTR if not. LPCSTR actually stands for "Long Pointer Constant String" if you understand your hungarian notation.
So let's see what's different from here?
First of all, to fix that, you'll just have to do something like this.
void CHelloDialogDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
AfxMessageBox(_T("abcde")); // or L"abcde"
OnOK();
}
The difference is that the current "abcde" you declared is not defined as a long pointer, but a "short" pointer (if I may say that). In order to declare it as a long pointer, you're required to add an additional "L", or use the macro _T(x) or _TEXT(x). I'm not going to go into details what long pointers are, I'm sure you can google it. L is just to convert the string of characters into wide-character literal which LPCSTR (str is actually talking about wchar_t).
Ultimately, it's all very confusing and I won't even want to delve into the messy explanations. But that's that. I might be wrong though, because it's been almost 6 or 7 years since I last coded in vanilla MFC/C++ windows programming. Forgive me. :P
galaxy_nus:
I'm starting to learn MFC with visual studio 2005. I've got a little problem.
In a dialog button OK, I added an event handler like this:
void CHelloDialogDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
AfxMessageBox("abcde");
OnOK();
}
it won't compile. The error is:
error C2665: 'AfxMessageBox' : none of the 2 overloads could convert all the argument types
When I change to this:
void CHelloDialogDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
CString astring = "abcde";
AfxMessageBox(astring);
OnOK();
}
The error is:
error C2440: 'initializing' : cannot convert from 'const char ' to 'ATL::CStringT<BaseType,StringTraits>'
It works well when i write this way:
void CHelloDialogDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
CString astring;
astring = "abcde";
AfxMessageBox(astring);
OnOK();
}
Could anyone show me what's wrong with my code?
Thank you so much.//aladdin: So pls. pay attention to this problem while the current project is using "Unicode" character set.