Thread: Windows newbie question

  1. #1
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115

    Question Windows newbie question

    I was wondering if anyone can tell me what is wrong with this code:
    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT uiMessage,
                             WPARAM wParam, LPARAM lParam);
    void Display_Windows_Error(void);
    
    HINSTANCE hInstGlobal;
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE PrevInstance, 
                         LPSTR lpCmdLine, 
                         int nShowCmd)
    {
       WNDCLASS WndClass;
    
       WndClass.style = 0;
       WndClass.cbClsExtra = 0;
       WndClass.cbWndExtra = 0;
       WndClass.lpfnWndProc = WndProc;
       WndClass.hInstance = hInstance;
       WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
       WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
       WndClass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
       WndClass.lpszMenuName = 0;
       WndClass.lpszClassName = "WinProgram4";
       
    
       if(RegisterClass(&WndClass) == 0)
           return(1);
       HWND hWindow;
       hWindow = CreateWindow(WinProgram4","Window",
                              WS_OVERLAPPEDWINDOW,0,0
                              400,400,NULL,NULL,hInstance,NULL);
       if(hWindow == NULL)
       {
          Display_Windows_Error();
          return(1);
       }
       ShowWindow(hWindow,nShowCmd);
       UpdateWindow(hWindow);
    
       MSG Message;
       while(GetMessage(&Message,NULL,0,0))
       {  DispatchMessage(&Message); }
       
       return(Message.wParam);
    }
    Basically the problem is this..... after the CreateWindow function I am checking to see if the hWindow is NULL and it is.Display_Windows_Error() is just a function to show me the what the error was. It calls GetLastError() function and FormatMessage() functions to tell me what went wrong. The error I am getting is "invalid Window Handle" and I can't figure out why. If anyone can tell me what is going wrong I would greatly appreciate it.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  2. #2
    Unregistered
    Guest
    hWindow = CreateWindow("WinProgram4","Window",
    WS_OVERLAPPEDWINDOW,0,0,
    400,400,NULL,NULL,hInstance,NULL);

    Syntax errors in the code you posted.

  3. #3
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    That is was not the problem. That is just my bad typing. If it was a syntax that was the problem I would be a happy camper
    My problem is hWindow being NULL.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    8

    Post

    Just so you know, I'm Unregistered above.

    I know that was'nt THE problem, I was just stating that the code you posted had syntax errors.

    Regarding the code, I'm no expert but it seems pretty messed up lol. I tried to fix it.

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    One thing I noticed is that you never populated your hInstGlobal
    variable.

    Code:
    hInstGlobal=hInstance;
    WndClass.hInstance=hInstGlobal;
    CreateWindow(blah...blah..blah,,,hInstGlobal,...);
    You also left out WS_VISIBLE in the dwStyle param of the
    CreateWindow call.

    And your passing 0 as your WndClass.style. I could be wrong but
    I've never seen 0 passed there.

  6. #6
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Hmm ...... thanks for the advice. This code is from a book, not my own. hInstGlobal is used in the WndProc function. The code I posted was by no means complete. I just want to know why hWindow is NULL.
    Just so I know, jizzer, What is wrong with the code I posted. You said it seemed pretty messed up. In what way? I am a total newbie to windows programming so I don't know if the book I am using is the right way or wrong way.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  7. #7
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Not sure what book you're learning from, but I notice you're using
    WNDCLASS, CreateWindow and RegisterClass. These 3 have been
    superceded by WNDCLASSEX, CreateWindowEx and
    RegisterClassEX. Of course I'm not saying that using the older
    versions are the problem, just something to think about.

  8. #8
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Ahhh I see. Yea I knew about that from reading microcraps help documents. Thanks for the info though
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  2. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  3. General question about Windows resources
    By Boomba in forum Windows Programming
    Replies: 2
    Last Post: 07-19-2004, 09:36 PM
  4. newbie question about using windows app in Dev C++
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2002, 10:50 PM
  5. Windows question (NOT MS)
    By Music_Man in forum Game Programming
    Replies: 2
    Last Post: 12-31-2001, 02:15 PM