Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 529
Ashton Avatar asked Oct 05 '22 13:10

Ashton


1 Answers

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.

like image 178
Abhineet Avatar answered Oct 23 '22 10:10

Abhineet