Beginner Question: CreateWindowEx Fails.

This is a discussion on Beginner Question: CreateWindowEx Fails. within the Windows Programming forums, part of the Platform Specific Boards category; Hi folks I'm only a learner. Been working through The Forgers WinAPI tutorial and sorting errors I make as I ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    11

    Beginner Question: CreateWindowEx Fails.

    Hi folks

    I'm only a learner. Been working through The Forgers WinAPI tutorial and sorting errors I make as I go along is a great way to learn. But now I'm a bit stumped.

    I've got a program that compiles, RegisterClassEx goes OK, but CreateWindowEx fails - and I can't for the life of me see why

    Can anyone point me in the direction of how to debug this kind of problem? I tried using GetLastError() to find more info, but silly me, I don't even know how to display the Integer it returns in a Message Box.

    I don't mind working things through for myself but would appreciate a little help with this one.

    Thanks in Advance

    dicky

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> how to display the Integer it returns in a Message Box.

    Code:
    #include <stdio.h>
    ...
    char Error[10];
    int LastError;
    ...
    LastError = GetLastError();
    sprintf(Error, "%d", LastError);
    MessageBox(NULL, Error, "Error is", MB_OK);
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Or use something like this(not my code, author unknown):
    Code:
    #include <windows.h>
    #include <stdarg.h>
    #include <stdio.h>
    #include <tchar.h>
    
    int CDECL MessageBoxPrintf (TCHAR * szFormat, ...)
    {
         TCHAR   szBuffer [1024] ;
         va_list pArgList ;
    
         va_start (pArgList, szFormat) ;
    
         _vsntprintf (szBuffer, sizeof (szBuffer) / sizeof (TCHAR), 
                      szFormat, pArgList) ;
    
         szBuffer[(sizeof (szBuffer) / sizeof (TCHAR)) - 1] = TEXT('\0') ;
    
         va_end (pArgList) ;
    
         return MessageBox (NULL, szBuffer, TEXT("Debug Message"), 0) ;
    }
    Code:
    MessageBoxPrintf("The error returned was %d.", GetLastError());
    EDIT:
    Nul termination added as pointed out by JasonD.
    Last edited by anonytmouse; 05-25-2004 at 02:07 PM.

  4. #4
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    The code may have been taken from Petzold. Here is his code:

    Code:
    int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...)
    {
         TCHAR   szBuffer [1024] ;
         va_list pArgList ;
    
              // The va_start macro (defined in STDARG.H) is usually equivalent to:
              // pArgList = (char *) &szFormat + sizeof (szFormat) ;
    
         va_start (pArgList, szFormat) ;
    
              // The last argument to wvsprintf points to the arguments
    
         _vsntprintf (szBuffer, sizeof (szBuffer) / sizeof (TCHAR), 
                      szFormat, pArgList) ;
    
              // The va_end macro just zeroes out pArgList for no good reason
    
         va_end (pArgList) ;
    
         return MessageBox (NULL, szBuffer, szCaption, 0) ;
    }
    Notice that this code avoids buffer overflow by using the _vsntprintf function with appropriate use of the sizeof operator. However, the above code is not perfect, as it does not guarantee NULL termination. So, you should force this yourself by setting the last character in szBuffer to NULL. That reminds me, I should add this to my errata list for Programming Windows...

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    Thanks for the help guys - I did sort out the problem with my program.

    It turned out not to be that CreateWindowEx was failing as such.

    Actually I had some error in the structure of a Switch statement that was above this part of my source code (curly braces and break; statements in the wrong order) and that was causing the problem - I think my program was kinda "falling" out of the Windows Procedure section and into the create window section. Anyway I learned some more by the time I fixed it

    dicky

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  3. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 07:39 PM

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