• 10
• 9
• 13
• 10
• 18

# Passing Char arrays to MessageBox()

This topic is 3433 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to pass an array of chars (C style string) to the second parameter of a MessageBox like this char words[255]; sprintf_s(words, "Letters : %f", g_iLetters); MessageBox(g_hWnd, words, L"Words", MB_OK); I've seen people passing char arrays to this function before (succesfuly) but I can't seem to figure it out, any help would be appreciated.

##### Share on other sites
What is the exact error message or what happens if you do this?

But try out
MessageBox (g_hWnd, static_cast <char*> (words), ...

##### Share on other sites
Nope, same error of converting parameter 2 from a char* to LPCWSTR

##### Share on other sites
you need to convert words to a multi-byte char. Try using this instead of char WCHAR.

##### Share on other sites
Quote:
 Original post by Googol PL3XNope, same error of converting parameter 2 from a char* to LPCWSTR
You're compiling in unicode, but passing a non-unicode string. You'll need to use a wide-character version of sprintf (See the Documentation for swprintf_s), use MultiByteToWideChar to convert from non-unicode to unicode, or call the multi-byte version of MessageBox, and change the 3rd parameter to non-unicode too.

##### Share on other sites
Hey, thanks everyone. Although I'm having some trouble filling the parameters for the MultiByteToWideChar function. This is what I have so far:

MultiByteToWideChar(CP_ACP, 0, &words, sizeof(words), buffer, 100);

Is this right? Also, I'm not sure what buffer should be, because it's supposed to hold the output? Should it be wide char or just char?
thanks

EDIT: never mind, got it!

##### Share on other sites
It should be something like:
WCHAR buffer[100];MultiByteToWideChar( CP_UTF8, 0, words, strlen(words), buffer, 100);

Though it would probably be easier to just make words WCHAR and use wsprintf, or call MessageBoxA(g_hWnd, words, "Words", MB_OK);

##### Share on other sites
Neither of those methods appear to compile. I've changed this to calculate FPS, so now it looks like this:

float fFPS = (float)g_dwFrames / ( g_fEndTime - g_fStartTime );
char strFPS[255];
sprintf_s( strFPS, "FPS : %f", fFPS );
MessageBox( g_hWnd, strFPS, "FPS Count", MB_OK );

But this isn't working, can someone help?

##### Share on other sites
The proper way to call MultiByteToWideChar and its mirror WideCharToMultiByte is to call it twice. The first time you pass in no buffer and get the size needed as the return value. You then allocate a buffer in some way and call it again.

{  char const * src = "omgwtf";  int cch = MultiByteToWideChar(CP_UTF8, 0, src, -1, 0, 0);  std::vector<wchar_t> buf(cch);  MultiByteToWideChar(CP_UTF8, 0, src, -1, &buf[0], cch);  MessageBox(0, &buf[0], L"Yay", MB_OK);}

As for your confusion about string types, there are two kinds of character types and string literals in C++.
The first is the regular char, which represents possibly multi-byte text in the execution charset. Those can be initialized by string literals like "omgwtf".
Then there are wchar_t, which is a wide character whose width is determined by the platform you're on. On Linux and some other posixy platforms it's 32 bits unsigned and usually contains UTF-32. On Windows it's 16 bits unsigned and contains UTF-16. You initialize one of those with a wide string literal like L"omgwtfbbq".

Most Windows API functions come in two variants, an A and a W variant. MessageBox is actually a macro that resolves to either MessageBoxA or MessageBoxW, depending on if UNICODE is defined or not.

The A functions take multibyte character strings in the current codepage and the W functions take wide strings in UTF-16.

MultiByteToWideChar does exactly what the name says, it converts from one of the narrow codepages to a wide string. As the source may be in a multi-byte encoding, you may need more than one source char to get a wchar_t. Thus, you need to call it two times, the first time to determine the proper length.