Write in MessageBox

Jan 4, 2011 at 8:01pm
How can i write in Messagebox double or int numbers ?
I want to do like this...
//...
string s1="You have ";
string s2=" messages.";
string s;
for(int i= 0 ;i<10;i++)
{
stringstream os;
os<<i;
s=s1+os.str()+s2;
MessageBox(NULL,...,L"Blah blah",MB_YESNO);

}

//...

What should i write instead of ...?
How can I convert from string to const wcar_t * ?
Please tell me
P.S. And sorry for my english.

Jan 4, 2011 at 8:29pm
Jan 5, 2011 at 12:29am
You already have an std::string, so use its c_str() method.

std::string
http://www.cplusplus.com/reference/string/string/

std::string::c_str()
http://www.cplusplus.com/reference/string/string/c_str/
Last edited on Jan 5, 2011 at 12:29am
Jan 5, 2011 at 10:05am
I know that but MessageBox wants const wchar_t * not const char *...
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR'
Jan 5, 2011 at 1:18pm
MessageBox is #defined to either MessageBoxW when compiling in UNICODE mode,
or MessageBoxA when compiling in NON UNICODE mode.

In your case change MessageBox to read MessageBoxA - that will use the NON UNICODE
version of the function.
Jan 5, 2011 at 3:22pm
Thanx very much that helps me but however when i compile with visual studio 6.0 i can write in message box
MessageBox(NULL,s.c_str(),L"Blah blah",MB_YESNO);
and i havn't got any errors but when i compile with vs 9.0 i've got an error.
Jan 5, 2011 at 4:51pm
What happens if you write
MessageBoxA(NULL,s.c_str(), "Blah blah",MB_YESNO); //notice the L is removed
Jan 5, 2011 at 7:50pm
That works fine
Jan 5, 2011 at 7:50pm
when i compile with visual studio 6.0 i can write in message box
MessageBox(NULL,s.c_str(),L"Blah blah",MB_YESNO);
and i havn't got any errors but when i compile with vs 9.0 i've got an error.


VS6 is not defaulting to Unicode.

VS9 is.

MessageBox(NULL,s.c_str(),L"Blah blah",MB_YESNO); is wrong in any event. guestgulkan is correct -- you should be using MessageBoxA.

See this for details:

http://www.cplusplus.com/forum/articles/16820/
Jan 8, 2011 at 7:20am
Either use MessageBoxA, std::string and std::stringstream or use MessageBoxW, std::wstring and std::wstringstream together.
Topic archived. No new replies allowed.