By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,441 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,441 IT Pros & Developers. It's quick & easy.

MessageBox(...) only displays first character of input string

P: 3
System: Intel, Windows XP Pro, SP2
IDE: VC++ 6.0

Problem: *Very* simple program to create a MessageBox only ever displays the first character of the given string.

I checked the spec for the MessageBox function and I believe I am adhering to it. I have also done a search for this issue, but have come up empty handed. Perhaps my search parameters were at fault ...

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h> // added to make MessageBox work (esp w/MB_<code>s)
  2. #include <string> // added to make std wstrings work
  3.  
  4. int main( int argc, char* argv[] )
  5. {
  6.     std::wstring content = L"If this works, it will be a miracle.";
  7.     std::wstring title = L"This is a Message Box";
  8.  
  9.     // Generates box with only the first characters of each string - why?...
  10.     MessageBox(0, (LPCTSTR)content.c_str(), (LPCTSTR)title.c_str(), MB_OK);
  11.  
  12.     // Title: "Error" displays as expected, content still only displays first letter ...
  13.     MessageBox(0, (LPCTSTR)content.c_str(), 0, MB_OK);
  14.  
  15.     // Title: "Error" displays as expected, content still only displays first letter
  16.     MessageBox(0, (LPCTSTR)L"small", 0, MB_OK);
  17.  
  18.     return( 0 );
  19. }
More info:
I'm working with this silly-simple program in an attempt to debug a much more complicated issue in a different program. I am starting with as little as possible and trying to get it to work so I can confirm the minimum requirements to run MessageBox successfully.

I want to steer clear of using the conversion functions for other reasons and in this case I don't think the (LPCTSTR) should be doing bad things -- of course other opinions are welcome especially if they offer insight into the current issue.

Thank you for taking a look at this, and ahead of time for any advice you might have, even if you can just point me to a new reference.
Jul 13 '07 #1
Share this Question
Share on Google+
9 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
This code:
// Title: "Error" displays as expected, content still only displays first letter
MessageBox(0, (LPCTSTR)L"small", 0, MB_OK);
typecasts a whar_t* to an LPCTSTR. Unfortunately, these are not the same thing. Saying something is something else does not make it something else.

The fact that this is an LPCTSTR means you are using tchar.h and that means you need to use the correct macros and not typecast.
Most likely, your code should be:
Expand|Select|Wrap|Line Numbers
  1. // Title: "Error" displays as expected, content still only displays first letter
  2.     MessageBox(0, TEXT("small"), 0, MB_OK);
  3.  
Jul 14 '07 #2

P: 3
This code:


typecasts a whar_t* to an LPCTSTR. Unfortunately, these are not the same thing. Saying something is something else does not make it something else.

The fact that this is an LPCTSTR means you are using tchar.h and that means you need to use the correct macros and not typecast.
Most likely, your code should be:
Expand|Select|Wrap|Line Numbers
  1. // Title: "Error" displays as expected, content still only displays first letter
  2.     MessageBox(0, TEXT("small"), 0, MB_OK);
  3.  
Yes, I am aware of what casting means. I'm happy to try macros instead, hopefully you're right. -- Ill get back to you when I try it out to let you know.

Thanks!

Edit: more concise
Jul 15 '07 #3

P: 3
You were right, I have to use the conversion macros. That's a nasty piece of business with the addt'l includes and the USES_CONVERSION macro you need to add as well. But, lesson learned.

Thanks again for your help.
Jul 16 '07 #4

P: 86
actually, you can get your original code working quite easily...first some anlysis

In your example, you didn't include tchar.h and your main(int argc, char* argv[] ) function worked with char parameters which tells me your application is being compiled in ansi.

Because your program is being compile in ANSI, MessageBox (the macro) is using MessageBoxA, the ANSI version and not MessageBoxW, the Unicode version, so you need to specifically call it.

MessageBoxW(0, content.c_str(),title.c_str(), MB_OK);

And all will be fine.
Jul 16 '07 #5

100+
P: 114
Is it possible to use the MessageBox() function in C?
Jul 17 '07 #6

P: 86
Is it possible to use the MessageBox() function in C?
Sure, Windows API is mostly C code. MessageBox() specifically is a macro that based on whether your application is Unicode or note, resolves to being MessageBoxW() for Unicode or MessageBoxA() for ANSI.
Jul 17 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
Sure, Windows API is mostly C code. MessageBox() specifically is a macro that based on whether your application is Unicode or note, resolves to being MessageBoxW() for Unicode or MessageBoxA() for ANSI.
Please don't advise to code this way. The whole idea of MessageBoxA and MessageBoxW was to accommodate Unicode and ASCII from the same code.

There are converions for the entire C-string library and between CHAR and WCHAR.

Using MessageBoxA freezes you to ASCII. Using MessageBoxW freezes you to UNICODE.

Stick to the TCHAR conversions.
Jul 17 '07 #8

100+
P: 114
And is using MessageBoxA and etc any different?
Jul 17 '07 #9

P: 86
Please don't advise to code this way. The whole idea of MessageBoxA and MessageBoxW was to accommodate Unicode and ASCII from the same code.

There are converions for the entire C-string library and between CHAR and WCHAR.

Using MessageBoxA freezes you to ASCII. Using MessageBoxW freezes you to UNICODE.

Stick to the TCHAR conversions.
The fact he used std::wstring tied him to using Unicode, so to easily show a std::wstring he can use a MessageBoxW

Personally I would've used typedef basic_string<TCHAR> tstring; but he might have a reason for using wstrings in an otherwise ANSI application.

***Edit
One other note, the macros in TCHAR.h are not conversion Macros, they will not convert UNICODE to ASCII or vice-versa, they are just ifdef# that are based on whether UNICODE is defined or not. There is nothing in there that will allow him to show a std::wstring with MessageBox() as his code stands now.
Jul 17 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.