Thread: What is wrong with this code?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    73

    What is wrong with this code?

    I get no errors when I run this code, but my error message pops up. I tried taking the error message out, and no window popped up and nothing happened. What is wrong!

    main.cpp
    Code:
    #include <windows.h>
    #include "resource.h"
    
    LRESULT CALLBACK WinProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
    HWND MakeWindow(HINSTANCE hInstance);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR iCmdLine, int iShow)
    {
    	HWND hWnd;
    	hWnd = MakeWindow(hInstance);
    	if(!hWnd)
    	{
    		MessageBox(hWnd,"Failed to Create Window!","Fatal Error!",MB_ICONERROR);
    		UnregisterClass("Boodle",hInstance);
    		return NULL;
    	}
    	MSG iMsg;
    	while(1)
    	{
    		if(PeekMessage(&iMsg,NULL,0,0,PM_REMOVE))
    		{
    			TranslateMessage(&iMsg);
    			DispatchMessage(&iMsg);
    		}
    	}
    	UnregisterClass("Boodle",hInstance);
    	return iMsg.wParam;
    }
    
    LRESULT CALLBACK WinProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch(iMsg)
    	{
    		case WM_DESTROY:
    		{
    			PostQuitMessage(0);
    			break;
    		}
    	}
    	return DefWindowProc (hWnd, iMsg, wParam, lParam);
    }
    
    HWND MakeWindow(HINSTANCE hInstance)
    {
    	WNDCLASSEX wndClassEx;
    	wndClassEx.cbSize = sizeof(wndClassEx);
    	wndClassEx.style = CS_HREDRAW | CS_VREDRAW;
    	wndClassEx.cbClsExtra = 0;
    	wndClassEx.cbWndExtra = 0;
    	wndClassEx.hIconSm = (HICON)LoadImage(hInstance,MAKEINTRESOURCE(IDI_ICON1),IMAGE_ICON,0,0,LR_DEFAULTSIZE);
    	wndClassEx.lpszMenuName = NULL;
    	wndClassEx.lpfnWndProc = WinProc;
    	wndClassEx.hInstance = hInstance;
    	wndClassEx.hIcon = (HICON)LoadImage(hInstance,MAKEINTRESOURCE(IDI_ICON1),IMAGE_ICON,0,0,LR_DEFAULTSIZE);
    	wndClassEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	wndClassEx.lpszClassName = "Boodle";
    
    	RegisterClassEx(&wndClassEx);
    
    	HWND hWnd;
    
    	hWnd = CreateWindow("Boodle","Might of the Boodle!",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,650,600,NULL,NULL,hInstance,NULL);
    
    	if(!hWnd)
    		return NULL;
    
    	return hWnd;
    }
    resource.h
    Code:
    //{{NO_DEPENDENCIES}}
    // Microsoft Visual C++ generated include file.
    // Used by resource.rc
    //
    #define IDI_ICON1                       101
    
    // Next default values for new objects
    // 
    #ifdef APSTUDIO_INVOKED
    #ifndef APSTUDIO_READONLY_SYMBOLS
    #define _APS_NEXT_RESOURCE_VALUE        102
    #define _APS_NEXT_COMMAND_VALUE         40001
    #define _APS_NEXT_CONTROL_VALUE         1001
    #define _APS_NEXT_SYMED_VALUE           101
    #endif
    #endif
    The resource file just has my icon. I'm using MSVS .NET so I cannot give you the source for resource.rc because it won't show you.
    Last edited by ElWhapo; 01-22-2005 at 09:51 PM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I cannot give you the source for resource.rc<<

    Resource files are plain text.

    Looks like you've forgotten to show the window:
    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR iCmdLine, int iShow)
    {
      HWND hWnd;
      hWnd = MakeWindow(hInstance);
      if(!hWnd)
      {
        MessageBox(hWnd,"Failed to Create Window!","Fatal Error!",MB_ICONERROR);
        UnregisterClass("Boodle",hInstance);
        return 0;
      }
    
      ShowWindow(hWnd,SW_SHOW);
      UpdateWindow(hWnd);
      
      MSG iMsg;
      while(1)
      {
        if(PeekMessage(&iMsg,NULL,0,0,PM_REMOVE))
        {
          /*must check for WM_QUIT*/
          if (iMsg.message==WM_QUIT)
          {
            break;
          }
          TranslateMessage(&iMsg);
          DispatchMessage(&iMsg);
        }
      }
      UnregisterClass("Boodle",hInstance);
      return iMsg.wParam;
    }
    Or, you could just create the window with the WS_VISIBLE style.

    edit: You need to amend your message loop so that your program can exit properly.
    Last edited by Ken Fitlike; 01-23-2005 at 04:28 AM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    edit: You need to amend your message loop so that your program can exit properly.
    As long as the WM_CLOSE and WM_DESTROY messages are handled within the main windows procedure there is no need to change the message loop in the WinMain function to check for the WM_QUIT message. I have never had to add this code to a project in order to properly exit.

    I get no errors when I run this code, but my error message pops up. I tried taking the error message out, and no window popped up and nothing happened. What is wrong!
    Try doing some error checking when you call RegisterClassEx, there may be an error when you register the window class and your not checking for it.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by cyreon
    As long as the WM_CLOSE and WM_DESTROY messages are handled within the main windows procedure there is no need to change the message loop in the WinMain function to check for the WM_QUIT message.
    This would be true if ElWhapo were using GetMessage and not PeekMessage. Compile and test the unmodified and modified code to verify this for yourself.
    Last edited by Ken Fitlike; 01-23-2005 at 05:06 PM. Reason: due to good suggestion from cyreon
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    It was just a suggestion. So what would be the problem then? If his program is returning an error message (when he doesn't remove the error message out) then his MakeWindow is returning NULL is it not? I don't understand how his error message can be fixed by simply calling ShowWindow and UpdateWindow. That would not remove the error message popping up would it?

    Just curious as to the solution.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>It was just a suggestion<

    Of course - I'll remove that part of my last post.

    >>That would not remove the error message popping up would it?<<

    It's a good point. When I tried it the return value from the MakeWindow function was good: I didn't observe any error message (just tested it). The window is created invisible and needs to be shown. And the message loop needs to be amended in order for the application to quit properly. Did you get the NULL window handle and error message, if you've tried it, that is?

    If the NULL return value from the MakeWindow function still remains a problem then checking the return value from CreateWindow with GetLastError would probably be in order - and/or doing some error checking on the return value of RegisterClassEx as you have suggested.
    Last edited by Ken Fitlike; 01-23-2005 at 05:26 PM. Reason: removed superfluous line
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    No I guess I should have tested the code before opening my mouth lol
    I was just going on information that he posted in his original post. He did actually state that he was getting his error message popping up and that was the only error checking I could see him really doing (as far as actually outputting an error to let a user know that there was a problem). Thats why I was assuming that it couldn't have just been creating the window invisible. But he was infact missing that since he didn't have the WS_VISIBLE style, nor did he call the ShowWindow or UpdateWindow calls.

    Sorry for doubting your answer though if infact the return value from the MakeWindow function is good.

    Edit: Oh and thanks for correcting me on the PostMessage versus GetMessage calls. I didn't see that until you mentioned it.

    Edit: Errr... PeekMessage! haha, I must be having one large brain fart today.
    Last edited by cyreon; 01-23-2005 at 05:13 PM.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Sorry for doubting<<

    Nothing to apologise for. I make enough mistakes that a second opinion is often necessary. Hence:
    Caution: this person may be a carrier of the misinformation virus.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Actually, on reflection, cyreon's suggestion may well be valid: if you're still having a problem with the error message box then try setting the hCursor parameter of your WNDCLASSEX variable in your 'MakeWindow' function to a valid value, such as:
    Code:
    wndClassEx.hCursor = (HCURSOR)LoadImage(hInstance,IDC_ARROW, IMAGE_CURSOR,0,0,LR_SHARED);
    It may be that since it's uninitialised, it's being filled with garbage and the system is reading it as an invalid cursor handle.

    You'll still have to make the changes I mentioned earlier in the thread to display your window and ensure your application is capable of quitting properly, though.
    Last edited by Ken Fitlike; 01-25-2005 at 04:30 PM. Reason: typo
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM