MFC C++ How do I display a const char value in MessageBox?

11,015

First of all, are you really using MessageBox API that way. Check the MSDN Documentation. Now to your question,

char e[1000] = "HELLO";
CString msg;
msg.Format(_T("%S"), e); // Mind the caps "S"
MessageBox( NULL, msg, _T("Hi"), NULL );

I think, you do not even need to Format data here. You can use::

TCHAR e[1000] = _T("HELLO") ;
MessageBox( NULL, e, _T("Hi"), NULL ) ;

This way, if _UNICODE is defined, both TCHAR and MessageBox would get chosen as WCHAR and MessageBoxW and if not defined as char and MessageBoxA.

Share:
11,015
Ashton
Author by

Ashton

Updated on June 15, 2022

Comments

  • Ashton
    Ashton almost 2 years

    I hope that the title was good enough to help explain what is needed. After solving this much of my project should be done.

    When I did this

        char e[1000] = "HELLO";
        CString msg;
        msg.Format(_T("%s"), e);
        MessageBox(msg);
    

    the messagebox just show me random words like "㹙癞鞮㹙癞鞮" instead of the "HELLO" i wanted. How do I solve this problem??

    Helps would be appreciated. Thank You