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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sovht
    New Member
    • Jul 2007
    • 3

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

    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 ...

    [CODE=CCode]#include <windows.h> // added to make MessageBox work (esp w/MB_<code>s)
    #include <string> // added to make std wstrings work

    int main( int argc, char* argv[] )
    {
    std::wstring content = L"If this works, it will be a miracle.";
    std::wstring title = L"This is a Message Box";

    // Generates box with only the first characters of each string - why?...
    MessageBox(0, (LPCTSTR)conten t.c_str(), (LPCTSTR)title. c_str(), MB_OK);

    // Title: "Error" displays as expected, content still only displays first letter ...
    MessageBox(0, (LPCTSTR)conten t.c_str(), 0, MB_OK);

    // Title: "Error" displays as expected, content still only displays first letter
    MessageBox(0, (LPCTSTR)L"smal l", 0, MB_OK);

    return( 0 );
    }[/CODE]

    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.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Originally posted by sovht
    // Title: "Error" displays as expected, content still only displays first letter
    MessageBox(0, (LPCTSTR)L"smal l", 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:
    [code=c]
    // Title: "Error" displays as expected, content still only displays first letter
    MessageBox(0, TEXT("small"), 0, MB_OK);
    [/code]

    Comment

    • sovht
      New Member
      • Jul 2007
      • 3

      #3
      Originally posted by weaknessforcats
      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:
      [code=c]
      // Title: "Error" displays as expected, content still only displays first letter
      MessageBox(0, TEXT("small"), 0, MB_OK);
      [/code]
      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

      Comment

      • sovht
        New Member
        • Jul 2007
        • 3

        #4
        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.

        Comment

        • Darryl
          New Member
          • May 2007
          • 86

          #5
          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.

          Comment

          • Firecore
            New Member
            • Jul 2007
            • 114

            #6
            Is it possible to use the MessageBox() function in C?

            Comment

            • Darryl
              New Member
              • May 2007
              • 86

              #7
              Originally posted by Firecore
              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.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by Darryl
                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.

                Comment

                • Firecore
                  New Member
                  • Jul 2007
                  • 114

                  #9
                  And is using MessageBoxA and etc any different?

                  Comment

                  • Darryl
                    New Member
                    • May 2007
                    • 86

                    #10
                    Originally posted by weaknessforcats
                    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<TC HAR> 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.

                    Comment

                    Working...