MessageBox and int to string

Up front - 1st post and preview doesn't seem to work, and hope code tags do,

Hope this is not too basic a question, but how does Windows communicate an integer to a user? Converting to a std::string is not hard, but from there, it seems impossible to use an API function to output the string to the window. MessageBox does not seem to be the answer. Just want an informative message to the user here. Any help would be appreciated. I'm kind of new to the API. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

switch (wmId)							
	{
	case IDM_OPEN:
	{
	if(!g_data->ReadDataFile()) {		
	MessageBox(hWnd, _T("Load of type DATA FAILED"), lpCaption, 
			MB_OK | MB_ICONINFORMATION);
	break;
	}
// tried this
// int ends up in "result", but "result" can't be used anywhere (so far)
//	tconvert << g_data->datacount;	//ostringstream
//	result = tconvert.str();       	//normally std::string

// just hardcode it for now  
	TCHAR result[25] = _T("?? records were loaded.");  

	MessageBox(hWnd, result , lpCaption, MB_OK | MB_ICONINFORMATION);
	}
	break;

	case IDM_SAVE:
.


Your TCHAR / _T macros almost certainly default to being UNICODE on the more recent (like this century) versions of visual studio.

Example.
https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/c92bfb01-3dce-450a-ac33-2b9b231f19b8/using-stdstring-stdwstring-in-code-that-builds-for-both-mbcs-and-unicode

The ASCII versions of the Win32 API pretty much died when Microsoft abandoned Win98.
In your commented-out code, is result supposed to be a std::string, or what?
Adding to what salem c said, if your TCHAR is the "wide" char, then you want to use std::wstring.
You could do a fancy sort of typedef to get a "tstring" type, something like:
using tstring = std::basic_string<TCHAR>;
But it might be better to just stick with Windows "unicode".

Edit: Oh wow, I didn't realize I was just repeating what was in salem c's link.
One caveat: You should not put anything into the std namespace yourself. That is reserved. It might not break. But you shouldn't do it.
Last edited on
You need to know if you're compiling as Unicode or ASCII.

For Unicode:

Something like this (not tried):

1
2
3
4
5
std::wostringstream tconvert;

tconvert << g_data->datacount << L" records were loaded.";
const std::wstring result = tconvert.str();
MessageBoxW(hWnd, result.c_str() , lpCaption, MB_OK | MB_ICONINFORMATION);

Thanks all! Being totally new to Windows and somewhat new to C++, I would have never got that on my own.

salem c , Ganado -

yep UNICODE. Not messing with adding stuff to namespaces. This stuff (for me) is complicated enough!

seeplus -

code worked the first time - with or without the "L". Thanks.
Being totally new to Windows and somewhat new to C++

Avoid for now using the Windows API as if it were the plague, IMO. I'd advice getting comfortable with "straight" C++ first.

The Windows API can be very intimidating for someone without a good C/C++ programming base.

Even with a good understanding of C/C++ Windows can be intimidating.

MS keeps changing the API, making a lot of online resources about how to do Windows API programming have problems.

The "Bible" of C-based Win32 programming, Charles Petzold's "Programming Windows, Fifth Edition", published 1999, needs some tweaks to the included source code to work with the current Windows API.

There are a couple of good online C++ tutorials I'd recommend. The one here at CPlusPlus ( http://www.cplusplus.com/doc/tutorial/ ) hasn't been updated in years. Still good for learning the basics of the C++ core.

Another good tutorial that is updated is Learn C++: https://www.learncpp.com/

I prefer Learn C++, it shows how to use modern C++ practices such uniform initialization and range-based for loops.

Furry Guy - Thanks for all the good info! The Learn C++ looks good. Really excited about learning new stuff.
Topic archived. No new replies allowed.