简体   繁体   中英

2010 version MFC code doesn't work anymore on 2013, any suggestions?

How should I change this vs2010 code to work in vs2013?

This is part of MFC application. I have two Edit Control with CString variables m_Name, and m_Age. There is also a print button that if clicked it shows these two information on a Message Box.

void CMyProgramDlg::OnBUTTON_PRINT()
{
    UpdateData(TRUE);
    char szText[100];
    sprintf(szText, "Name: %s\n"\
                    "Age: %d",
                    m_Name, m_Age);
    MessageBox(szText, m_Name+"Message", NULL);

}

The problem is MessageBox() doesn't take char any more. So I converted to CString. But the new problem is the printed message only shows the first letter of the name and age. So if I put in 'Jack' for the name and '40' for the age, it only shows 'J' and '4'.

The new project apparently compiles in Unicode mode, so TCHAR is wchar_t , and all WinAPI functions accept wchar_t instead of char (or pointers to them).

More precisely: The macro MessageBox that expanded to MessageBoxA in the old project now expands to MessageBoxW , and where MessageBoxA accepted pointers to char , MessageBoxW expects pointers to wchar_t . This mechanism exists for all WinAPI "functions" that accept strings.

Go into the project's project properties and set "Character Set" under Configuration Properties -> C/C++ -> General to not set or multibyte instead of Unicode, then it should behave as before. Alternatively, use MessageBoxA instead of MessageBox to invoke the ANSI version explicitly, or change the code so that it uses TCHAR everywhere.

See https://msdn.microsoft.com/en-us/library/c426s321.aspx for details.

The VS 2013 no longer supports MBCS. So you have to convert your code to UNICODE:

UpdateData(TRUE);
CString szText;
szText.Format(_T("Name: %s\nAge: %d"), m_Name, m_Age);
MessageBox(szText, m_Name + _T("Message"), NULL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM