Many questions : MessageBox(

MessageBox(HWND_DESKTOP, magcharstr, headingcharstring, MB_OKCANCEL);

Questions: (visual c++ 2008 express)

1.) heading string to long truncates safely?
2.) heading string must be ended with \0?

3.) what is max length each strings?

4.) two end of lines will go to first and execute fine?

5.) if unknown chars before an ending \0 safe to execute
and print chars until the \0.


6.) messaage box 2 strings may use quotes without \0 char?

7.) Important:

Did read somewhere overlapping strings may cause unpredictable result. Is this a matter of strings work fine if there is a \0 and EVEN MORE THAN ONE \0? in functions and doesnt if a creating function does not get to a \0?

This idea seems to be mainly for strcopy( etc, and will be okay for MessagBox?


Challenging questions from,

Joshua.
I'm not sure if your question has more to do with strings in general than with message boxes...
http://www.cplusplus.com/reference/string/string/

All of these questions should be answerable by looking at the parameters and examples in:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645505%28v=vs.85%29.aspx
lpText [in, optional]

Type: LPCTSTR

The message to be displayed. If the string consists of more than one line, you can separate the lines using a carriage return and/or linefeed character between each line.

Where LPCSTR is a typedef for a long pointer to a constant string.

If you're dealing with char arrays then yes, you have to take care with the terminating '\0'. If you're dealing with strings then the terminating '\0' is taken care of for you in the background of the string object therefor it is not of your concern.

3-
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("My test string!");
  cout << "max_size: " << str.max_size() << "\n"; //max characters this string can hold
  return 0;
}

6- You can only pass in 1 string for each of the text field and the caption (main top bar). If you need to show the content of 2 strings in the text field then you have to combine them through a 3rd string or other ways and then pass the result into the function.
Last edited on
@soranz, the @OP was asking about MessageBox API text length limit, not std::string limit.
MessageBox API text length limit

I've heard that it is 1024 char but don't know for certain.
I created a simple Win32 app and made it form driven and then added 2 buttons named Button1 and Button2.

Then I did something simple like this for their functionality:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void CMessageTextTest3Dlg::OnBnClickedButton1()
{
	wchar_t Title2k[2000] = {'\0'};
	wchar_t String2k[2000] = {'\0'};
	wmemset(Title2k, 69, 1999); // gonna party like it's... 
	wmemset(String2k, 69, 1999);
	Title2k[1999] = '\0';
	String2k[1999] = '\0';

	MessageBox(String2k, Title2k);
}


void CMessageTextTest3Dlg::OnBnClickedButton2()
{
	wchar_t Title4k[4000] = {'\0'};
	wchar_t String4k[4000] = {'\0'};
	wmemset(Title4k, 69, 3999);
	wmemset(String4k, 69, 3999);
	Title4k[3999] = '\0';
	String4k[3999] = '\0';

	MessageBox(String4k, Title4k);
}


Worked just fine. So, there isn't any limit in the native C++, native Win32 world that I could find, though I didn't push it. I'm sure there's some practical limit. I mean, at some point you run out of memory.

As for the rest of your questions:

1) Heading string too long displays as "blah blah bla..."

2) All c-style strings must end in '\0'. You may SOMETIMES get by with not doing so completely by accident but never count on it.

3) See above.

4) I didn't test this, but you can do so using the test code I gave you above. If just '\n' doesn't display properly, you may have to look for it and add a '\r', as well.

5) Under Windows they'll just display crap, but yes, they should be fine to print. That said, TEST, TEST, TEST!

6) Yes, the above test code could have as easily initialized to a 2000 line string of 'E's in quotes with no \0 (it's implied under Windows).

7) The note about overlapping strings is when you're trying to do an in place reduction in string size or trying to do funky stuff like "borrow" from one string to fill another. You should **NEVER** do this! Always make every string have it's own buffer unless you are FORCED to do otherwise. And, if you are forced to do so seriously consider a rewrite/redesign. The only time I can think of that this makes SOME sense is if there is a resource issue (memory or performance).

Xen
Last edited on
here are bunch of questions....
Thank you all much.

Joshua
Topic archived. No new replies allowed.