Ashton
Ashton

Reputation: 83

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

Upvotes: 5

Views: 11617

Answers (1)

Abhineet
Abhineet

Reputation: 5399

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.

Upvotes: 4

Related Questions