473,408 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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
9 7919
weaknessforcats
9,208 Expert Mod 8TB
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
sovht
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
sovht
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
Darryl
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
Firecore
114 100+
Is it possible to use the MessageBox() function in C?
Jul 17 '07 #6
Darryl
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
9,208 Expert Mod 8TB
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
Firecore
114 100+
And is using MessageBoxA and etc any different?
Jul 17 '07 #9
Darryl
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

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

Similar topics

4
by: Drew Lettington | last post by:
I'm making a simple call to display error messages in a MessageBox from a Windows form and the MessageBox is not behaving in a modal fashion. My modal form displays, the user clicks a button and...
2
by: Cantekin Guneser | last post by:
in my program when a button clicked , a process starts, but i need to take two value, becouse of this, i use a new windows form, to make my program more userfriendly, i was thinking is it possible...
4
by: Tressa | last post by:
I have a messagebox that I only want to pop up only if it is not already being displayed on the screen. My code is still poping up the messagebox even though it is on the screen. What am I doing...
1
by: Daniel Passwater via DotNetMonster.com | last post by:
I'm a newbie. So please be patient. I'm working on a C# app in compact framework for a PDA. It connects to a device and extracts and updates data. I need to double check with the user before I...
10
by: Russ | last post by:
I've been trying to figure out how to show a simple messagebox with an OK button in my web client program (C#). I have looked at every reference to JScript and MessageBox that seemed even remotely...
10
by: Andrew | last post by:
Hi, I have a messagebox that pops up due to an event. I did it in javascript. ie. alert("Time's up. Assessment Ended"); I want to capture the OK and Cancel events of this alert messagebox. My...
3
by: Thomas Beyerlein | last post by:
Is there a way to put some of the text typed into a messagebox string on a new line? This is for user readability only. Thanks Tom *** Sent via Developersdex http://www.developersdex.com...
2
by: =?Utf-8?B?ZGxpbmdn?= | last post by:
I have a simple c# windows form with a textbox and button that, when clicked, displays a simple MessageBox. With the cursor in the textbox, I can select the Japanese language and desired Input...
1
by: Lancer | last post by:
Hi all, I need to print (send to printer...) a string that has showed in a MessageBox. When I click Yes, I want to print str. *********************************** CODE...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.