Thread: Problem with XP

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Problem with XP

    Got this code out of a windows programming book. The problem is the window doesn't open, just the error message box. How can I change the code to work with xp?

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
                        
    {
        static TCHAR szAppName[] = TEXT ("HelloWin");
        HWND        hwnd;
        MSG         msg;
        WNDCLASS    wndclass;
        
        wndclass.style        = CS_HREDRAW | CS_VREDRAW;
        wndclass.lpfnWndProc  = WndProc;
        wndclass.cbClsExtra   = 0;
        wndclass.cbWndExtra   = 0;
        wndclass.hInstance    = hInstance;
        wndclass.hIcon        = LoadIcon (NULL, IDC_ARROW);
        wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
        wndclass.lpszMenuName = NULL;
        wndclass.lpszClassName = szAppName;
        
        if ( !RegisterClass (&wndclass))
            {
            MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                        szAppName, MB_ICONERROR);
            return 0;
            }
        
        hwnd = CreateWindow (szAppName,
                             TEXT ("The Hello Program"),
                             WS_OVERLAPPEDWINDOW,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             NULL,
                             NULL,
                             hInstance,
                             NULL);
    
        ShowWindow (hwnd, iCmdShow);                     
        UpdateWindow (hwnd);
        
        while (GetMessage (&msg, NULL, 0, 0))
            {
            TranslateMessage (&msg);
            DispatchMessage (&msg);
            }
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC             hdc;
        PAINTSTRUCT     ps;
        RECT            rect;
        
        switch (message)
            {
            case WM_CREATE:
                    //PlaySound (TEXT ("hellowin.wave"), NULL, SND_FILENAME | SND_ASYNC);
                    return 0;
                    
            case WM_PAINT:
                    hdc = BeginPaint (hwnd, &ps);
                    GetClientRect (hwnd, &rect);
                    DrawText ( hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
                               DT_SINGLELINE | DT_CENTER | DT_VCENTER);
                    EndPaint (hwnd, &ps);
                    return 0;
                    
            case WM_DESTROY:
                    PostQuitMessage (0);
                    return 0;
            }
        return DefWindowProc (hwnd, message, wParam, lParam);
    }
    Yes I know its pretty basic, just trying to get it working before I add on more.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    So RegisterClass fails? First thing's first. GetLastError() will tell you why it failed (theoretically).
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    7
    RegisterClass(Ex) returns NULL if it fails, for any reason. Other things can cause this function call to fail, specifically, incorrect parameters in the WNDCLASS(EX) structure. At first glance, the only thing i might suggest is adding the following:

    after WNDCLASS wndclass:

    ZeroMemory(&wndclass,sizeof(WNDCLASS));
    wndclass.cbSize=sizeof(WNDCLASS);

    The Windows API is very picky about such things, HOWEVER, befopre trying that, you should check that RegisterClass is failing because of an incorrect parameter. The error messages from API function calls are stored by the system, and can be retrieves with GetLastError(), this error then can be looked up. You can also retrieve an error message for the code. Warning though, Win16 legacy OS's (Win95,98,ME) do not specify that the error code will be set.

    You can test the error code by doing something like this by replacing the if(registerblahblh) { } ; section with something like:

    Code:
    if(!RegisterClass(&wndclass))
    {
         LPVOID lpMsgBuf;
         if (!FormatMessage( 
              FORMAT_MESSAGE_ALLOCATE_BUFFER | 
              FORMAT_MESSAGE_FROM_SYSTEM | 
              FORMAT_MESSAGE_IGNORE_INSERTS,
              NULL,
              GetLastError(),
              MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
              (LPTSTR) &lpMsgBuf,
             0,
             NULL))
         {
              // do summat, format message failed
              MessageBox(NULL,TEXT("FormatMessage Failed"),
                TEXT("Unable to retrieve error message text"),MB_ICONINFORMATION);
              return;
         }
    
         MessageBox( NULL, (LPCTSTR)lpMsgBuf,
           TEXT("Error - RegisterClass Failed"), MB_OK | MB_ICONINFORMATION );
    
         LocalFree( lpMsgBuf );
         
         return;
    };
    For more info on these techniques you might want to look at:

    http://msdn.microsoft.com/library/en...tlasterror.asp

    and

    http://msdn.microsoft.com/library/en...matmessage.asp

    (the format message code i used as an example was taken directly from the above article)

    // forgot unicode macros
    Last edited by Tada; 08-12-2003 at 01:53 PM.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    If RegisterClass doesn't fail, try changing the second parameter of ShowWindow to SW_SHOWNORMAL, instead of iCmdShow.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I'd put my money on this:
    wndclass.cbSize=sizeof(WNDCLASS);
    probably
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    I didn't even realize that was missing. I'm just used to the compiler adding all that stuff in for me.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Added some more code in and am getting error code 87. in winerror.h I'm guessing that the line
    #define ERROR_INVALID_PARAMETER 87L
    is the error.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    wndclass.cbSize=sizeof(WNDCLASS);
    Tried adding that in and I get the error:
    struct _WNDCLASSA' has no member named `cbSize'

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    oh yeah, that's part of WNDCLASSEX. I honestly don't know what the problem is. It seems as if it should work.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    7
    If it is an invalid parameter then my only suggestions are:

    wndclass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);

    This is the reccommended method to set the hbrBackground member, although i'm not sure if it will solve your problem.
    (use COLOR_BTNFACE for a typical light ish gray background)

    wndclass.style=0;

    For normal usage windows, class styles aren't generally required and tend to consume more resources than are actually required.

    Clutching at straws really, bit stumped. I'll give compiling it a try and see what else i can come up with.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    7
    Ok ignore the suggestions above, just do the following:

    after:

    WNDCLASS wndclass;

    add:

    ZeroMemory(&wndclass,sizeof(WNDCLASS));

    and all should be well.

    The problem was an uninitialised member of wndclass. using ZeroMemory is always a good way to diagnose these errors because it in effect sets all the structure members to NULL (well, 0, but that's irrelevant). Therefore windows ignores the parameters you havn't set. The one you missed out was the class cursor, hCursor.
    To remove the need for the ZeroMemory gubbins, you actually only need to add:

    wndclass.hCursor=NULL;
    or more preferably:
    wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);

    because otherwise, the hCursor member would be set to any old garbage that was on the stack before wndclass. With large and complicated Windows API structures, using ZeroMemory can be easier, although it does have a penalty because it has to explicitly set all the bytes to zero (it probably works with DWORD's in effect as most API structures are DWORD aligned, but that is another story)
    Last edited by Tada; 08-12-2003 at 02:33 PM.

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that's strange, it looks like he initialized all of the members already.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    7
    Heh yeh, sorry i have a habbit of editing posts Only just read your post.

  14. #14
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    ah yes. hCursor. For some reason I didn't catch that one.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thanks, that did it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with ezwin.h in XP
    By Good0m3n in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2004, 01:03 PM
  2. Big Problem with XP (booting)
    By axon in forum Tech Board
    Replies: 11
    Last Post: 03-17-2004, 02:34 AM
  3. Stanby wake up problem [windows xp resnet]
    By Boomba in forum Tech Board
    Replies: 6
    Last Post: 01-06-2004, 08:51 PM
  4. XP Autoplay problem
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-09-2002, 08:59 AM