Like Tree1Likes
  • 1 Post By grumpy

Multiple errors where none occurred before

This is a discussion on Multiple errors where none occurred before within the Windows Programming forums, part of the Platform Specific Boards category; greetings I have just installed Visual Studio 2012 and after MANY years of doing nothing I tried out some of ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    13

    Multiple errors where none occurred before

    greetings I have just installed Visual Studio 2012 and after MANY years of doing nothing I tried out some of my old code which used to work fine but now generates multiple errors. Even MessageBox says I need four parameters but I can get around this by using _T( ) around the text?

    TCHAR instead of char stops errors that did not occur before in declaring variable names?

    example of what I successfully used before....

    Code:
        char name[81] = "";
        int len = 0x0, i;
        
        GetDlgItemText(IDC_EDIT1, name, 80);    // get the name entered 
    
        len = strlen(name);                        // check something entered
    
        if(!len)                                // if not exit function
        {
                 MessageBox("You must enter at least one character!", "Error!", MB_OK);
                 return;
        }
    I know this is a basic stupid question but at the moment I can't see the forest for the trees.

    Surely things haven't changed so much in the passing years it must be something I have set up wrong in Visual Studio?

    Any tips muchly appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    Note to mods: This is a windows programming question, and belongs in that forum, not in the C++ forum.

    You're barking up the wrong tree with using _T() macro or TCHAR type. Those are related to default character widths (eg allowing code to work whether built using unicode or not), not to fixing problems with number of arguments a function is called with.

    MessageBox() has always accepted four arguments - at least, since the first version of the windows API that shipped with Windows 1.

    However, historically, C compilers allowed usage of undeclared functions: when code calls a function that had not been declared, the compiler would assume the function accepts a variable argument list. The C++ standard made that flatly illegal. Using that feature has been consider bad practice in C for a few decades, so such things have been deprecated in recent C standards.

    So, in short, the code you used "successfully" before was actually broken, and more recent compilers now detect that it is broken.

    The argument your code is missing is actually the first: a pointer to a window that will own/manage the MessageBox. That pointer can be NULL (indicating no owner window), so the easiest solution would be to
    Code:
         MessageBox(0, "You must enter at least one character!", "Error!", MB_OK);
    or (more completely)
    Code:
         MessageBox((HWND)NULL, "You must enter at least one character!", "Error!", MB_OK);
    It is mandatory if compiling as C++ (and really good practice if compiling as C) to ensure the compiler can see a declaration of MessageBox() before the code that calls it. That can be achieved by #include <winuser.h> (which is Microsoft specific, but shouldn't cause problems, assuming you are building a windows application).
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Sorry for the post in the wrong section I really need to have a look before I jump but thanks very much for your reply.

    I actually copied and pasted your

    Code:
    MessageBox((HWND)NULL, "You must enter at least one character!", "Error!", MB_OK);
    but am still recieving the following error message?

    error C2660: 'CWnd::MessageBoxW' : function does not take 4 arguments

    I am under the impression wrongly likely that I am not calling the "MessageBoxW" function ?

    appreciate your time and thanks.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    Oh, okay. It would have helped if you had said you were using MFC.

    MessageBox is a function in the windows API (currently win32 API) and, given that you didn't specify things further and references to need 4 arguments, I assumed you were not using MFC.

    You're using a MessageBox that is a member function of CWnd, which is a MFC class. MFC is a class library, which is built on top of the win32 API.


    I suspect you're taking code which was developed using MFC, and putting parts of it into a context that doesn't involve using MFC.


    Either way, when building applications for windows, one of the build options is using wide characters (unicode) or not. When you build for unicode, MessageBox is a macro that expands to MessageBoxW() [I can't remember, offhand, what MessageBox expands to if not building for unicode, but it is a different function]. MessageBoxW() does require the two string arguments to be wide character strings, hence need to use _T() macro (the _T() macro does things differently as well, depending on whether you're building for unicode or not). That is true for both win32 and MFC (the only difference being that win32's MessageBox requires 4 arguments to MFC's CWnd::MessageBox() requiring 3).

    Presumably your code was build without unicode (which means it either predated unicode, or the developer simply set build options to not use unicode and didn't bother further). So you need to look at your project options, and turn off unicode (or wide character) support, or use the _T() macro.


    Either way, you need to make sure project build options are consistent with what your code is assuming. The simplest is probably to turn off unicode support, in the first instance. (Historically, unicode was introduced later, and became a default). That is a Microsoft change, not a C/C++ change.

    So, in short, you need to look at your code and building it in a holistic manner.
    Last edited by grumpy; 03-16-2013 at 09:45 PM.
    iMalc likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    You are a legend Sir I disabled the Unicode support like you suggested and all of a sudden everything of my old stuff is working again

    Thank you so much for your time and have a great day mate

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple errors on one line - C
    By djdato in forum C Programming
    Replies: 12
    Last Post: 05-15-2010, 11:48 PM
  2. Multiple syntax errors
    By Devolution in forum C Programming
    Replies: 1
    Last Post: 03-25-2009, 10:37 PM
  3. A Miracle has occurred in the realm of Hot Dogs
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-08-2004, 03:18 PM
  4. multiple declarations of functions errors
    By netwizio in forum C Programming
    Replies: 4
    Last Post: 03-07-2004, 08:02 AM
  5. Paks (Hungary) Nuclear Power Plant - fuel damage occurred on 10 11 April 2003
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 06-17-2003, 05:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21