Thread: Messageboxes will work in win2k, fail in XP

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    Messageboxes will work in win2k, fail in XP

    As a sort of introductory app to windows applications in C++, I've written a short program that displays a few messageboxes, and plays around with the buttons in them (OK, OKCANCEL, etc).

    It works beautifully in Windows 2000. However, it silently fails (i.e. no error messages) in XP and I'm not sure why that is. I use both OSes, so I want to be able to run the app in both of them.

    Here's the important part of the code: (I've removed a few things from the code, but trust me I didn't omit anything pertinent)

    Code:
    //sorry for the immature variable/window naming :P
    #include <windows.h>
    
    int WINAPI
    WinMain(HINSTANCE hInst, 
    	HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine,
            int nCmdShow)
    {
        int lol = MessageBox (NULL, "This is a message box", 
                    "OMG", MB_OKCANCEL);
        if (lol == 2) //if user clicked cancel
            return 0;
        MessageBox (NULL, "Why did you select cancel?", 
                    "ERROR", 0 + 48);
        return 0;
    }
    Is there something in that code that's win2k specific? Is it to do with the OS it was compiled on? Ideally I'd like some code that runs the same on both OSes, but if I have to, I'll settle with compiling a seperate version for each.

    Any help is appreciated!

    -Rippy

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Really stupid question, but did you build in release mode?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
        if (lol == 2) //if user clicked cancel
            return 0;
        MessageBox (NULL, "Why did you select cancel?", 
                    "ERROR", 0 + 48);
    A good suggestion is: don't use magic numbers. Use appropriate constants. Otherwise it'll be hard for anyone to read the code.
    For example, I have no idea what 0 + 48 will display.
    Last edited by Elysia; 02-21-2008 at 04:08 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to the Windows programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by Elysia View Post
    A good suggestion is: don't use magic numbers. Use appropriate constants
    Quoted for truth.

    Use the constants specified for MessageBox on msdn, ie instead of
    Code:
    if (lol == 2)
    prefer
    Code:
    if (lol == IDCANCEL)
    But be sure to read the 'return value' comments on that msdn page to ensure your message box fits that description.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Actually, I had a comment on that very line like this:

    Code:
    MessageBox (NULL, "Why did you select cancel?", 
                    "ERROR", 0 + 48); //0 makes it an OK message, 48 adds the exclamation mark icon
    Seeing as I'm new to this, I've been kind of inconsistent (my very first test messagebox used MB_OK instead of 0). But I see how using the appropriate names is a bit more effective than adding a comment.

    As for the "lol == 2" thing, I'll admit that that was a complete shot in the dark. I just knew that Messagebox returned a value, so I tried a few values to see which corresponded to each button.

    I don't have access to my code at the moment, but hopefully that's the problem. I'll also check to see if I'm building it properly as the first poster asked, because I don't remember explicitly setting anything.

    Thanks for all the replies =D

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Rippy View Post
    Actually, I had a comment on that very line like this:
    Code:
    MessageBox (NULL, "Why did you select cancel?", 
                    "ERROR", 0 + 48); //0 makes it an OK message, 48 adds the exclamation mark icon
    Clarity to the programmer is part of it. Clarity to the compiler is the other.

    The macro might change values with different OSes for example.

    So use the macro, constant, or whatever, not the literal.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The Win32 docs will show you specifically what values MessageBox() will take. Some samples are:

    Buttons
    MB_YESNO
    MB_OK (default)
    MB_OKCANCEL

    Message Types
    MB_ICONEXCLAMATION
    MB_ICONINFORMATION
    MB_ICONWARNING
    MB_ICONQUESTION

    Return values
    ID_OK
    ID_YES
    ID_NO
    ID_CANCEL


    You can achieve the desired buttons with the desired type by ORing these constants.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Ok, well, my googling for a release build in Dev-C++ gave me next to no results. What do you mean by that?

    I've applied the other suggestions already, though unfortunately I again don't have access to my XP machine (backup restore gone wrong, heh). Thanks though, the code is definitely more clear now.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    My guess is that your winXP machine is missing a required .DLL to run the application, but then again i don't understand gcc as well as VS.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    It works! I think it was somehow to do with using those macros. Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  2. Does reading file with fstream not work on xp?
    By pinkcheese in forum C++ Programming
    Replies: 13
    Last Post: 04-11-2002, 08:39 AM
  3. Will Dev-C++ work with XP?
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2002, 11:48 PM
  4. Replies: 6
    Last Post: 01-07-2002, 02:46 AM
  5. ClipCursor() in XP - Dam it dont work!!
    By (TNT) in forum Windows Programming
    Replies: 0
    Last Post: 11-11-2001, 10:05 AM