Jump to content
  • Advertisement
Sign in to follow this  
Googol PL3X

Passing Char arrays to MessageBox()

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
What is the exact error message or what happens if you do this?

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

Share this post


Link to post
Share on other sites
Quote:
Original post by Googol PL3X
Nope, 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
I understand that, but I've seen programms with exactly the same libs, includes and stuff in C++ DirectX9 that can call with out having to put L before literal strings or convert stuff for the windows message boxes!! Why is this, it's very frustrating!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!