winAPI hello world won't quit !need help to solve it

This is a discussion on winAPI hello world won't quit !need help to solve it within the C++ Programming forums, part of the General Programming Boards category; I just started learning the Win API, this is the first code i wrote by referring some various tutorials i ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    winAPI hello world won't quit !need help to solve it

    I just started learning the Win API,
    this is the first code i wrote by referring some various tutorials i found in the internet...

    i have a problem, the program won't close. when i click the X on the corner the window is destroyed, but the process still keeps running ( i can see it on the Task Manager )...

    how do i make the process end/quit... dosen't WM_QUIT do that??

    Code:
    #include <Windows.h>
    
    // message handler
    LRESULT CALLBACK WndProc(HWND hWindow, UINT iMessage,WPARAM wParam, LPARAM lParam);
    
    
    
    int WINAPI WinMain(
    		HINSTANCE hInstance, // Handle to the current instance
    		HINSTANCE hPrevInst, // Handle to the previous instance
    		LPSTR lpCmdLine,		// Pointer to the command line
    		int nShowCmd			// Shows the state of the window
    		)
    {
    	WNDCLASS kWndClass;	// window object
    
    	// set visual properties of the window
    	kWndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    	kWndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	kWndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    
    	// set system properties of the window
    	kWndClass.hInstance = hInstance;
    	kWndClass.lpfnWndProc = (WNDPROC)WndProc;
    	kWndClass.lpszClassName = TEXT("01 Basic Window");
    
    	// set extra properties
    	kWndClass.lpszMenuName = NULL;
    	kWndClass.cbClsExtra = NULL;
    	kWndClass.cbWndExtra = NULL;
    	kWndClass.style = NULL;
    
    	// register the window object
    	if(!RegisterClass(&kWndClass))
    	{
    		MessageBox(NULL, TEXT("Window Registration Failed!"), TEXT("Error!"),
                MB_ICONEXCLAMATION | MB_OK);
    		return -1;
    	}
    
    	HWND hWindow;	// The window
    
    	hWindow = CreateWindow(
    				TEXT("01 Basic Window"), // Registered class name
    				TEXT("A Blank Window"),	// Application window name
    				WS_OVERLAPPEDWINDOW | WS_VISIBLE,	// Window style
    				CW_USEDEFAULT,  // Horizontal position of the window
    				CW_USEDEFAULT,  // Vertical position of the window
    				CW_USEDEFAULT,  // Window width
    				CW_USEDEFAULT,  // Window height
    				NULL,           // Handle to the parent window
    				NULL,           // Handle to the menu the identifier
      			        hInstance,      // Handle to the application instance
    				NULL            // Pointer to the window-creation data
    				);
    
    	// Check if it failed to create the window successfully
    	if(!hWindow)
    		return FALSE;
    
    	ShowWindow(hWindow,nShowCmd);
    	UpdateWindow(hWindow);
    
    	MSG kMessage;	// Message
    
    	// Message Loop
    	while( GetMessage(&kMessage,hWindow,0,0)!= FALSE )
    	{
    		TranslateMessage(&kMessage);
    		DispatchMessage(&kMessage);
    	}
    
    	return kMessage.wParam;
    }
    
    
    LRESULT CALLBACK WndProc(HWND hWindow, UINT iMessage,WPARAM wParam, LPARAM lParam)
    {
    	switch(iMessage)
        {
            case WM_CLOSE:
                DestroyWindow(hWindow);
            break;
    
    	case WM_DESTROY:
                PostQuitMessage(0);
            break;
    
            default:
                return DefWindowProc(hWindow,iMessage,wParam,lParam);
        }
    
        return 0;
    }
    --------------------------------------------------------------------------------------------------------------------------------------
    by the way,
    i am using windows 7 32 bit.

    do i have to use WNDCLASSEX other than WNDCLASS in win 7 ??

    i now tried the code from this site,
    Tutorial: A Simple Window

    and it worked correctly...

    That code is using WNDCLASSEX and relevent "Ex" functions....
    --------------------------------------------------------------------------------------------------------------------------------------
    Last edited by Deamonpog; 05-14-2011 at 06:18 AM. Reason: added to the end some more info

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    23
    Check the return value of GetMessage or take a look at PeekMessage, Strange the window messgaes look fine, can you verify that WM_DESTROY is being called.

    WNDCLASS should be fine msdn states: "You can still use WNDCLASS and RegisterClass if you do not need to set the small icon associated with the window class.". but if it works now maybe it was a msvc prob or a breakpoint
    Last edited by pYro; 05-14-2011 at 06:25 AM.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Quote Originally Posted by pYro View Post
    Check the return value of GetMessage or take a look at PeekMessage, Strange the window messgaes look fine, can you verify that WM_DESTROY is being called
    thanks pYro,

    yes i tried that, i am using VC++ too. so i put some breakpoints and followed the execution of code... WM_CLOSE and WM_DESTROY are passed... but the process dosent end.. only the window gets destroyed... and sometimes when i right click on the title bar and select close from the drop down menu, it works... but the X on the corner dosent work like that...

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Oki,
    I found that if i replace
    Code:
    GetMessage(&kMessage,hWindow,0,0)
    by this,
    Code:
    GetMessage(&kMessage,NULL,0,0)
    it works....

    but is it OK to do it... will then the other messages work correctly after putting it like that???

    i tried putting
    Code:
    hWindow=NULL;
    in WM_CLOSE and WM_DESTROY, but they both didn't work...

    or i can use PeekMessage and check
    Code:
    kMessage.message==WM_DESTROY
    before i put the kMessage to the GetMessage() rite...?

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    23
    gotit, GetMessage returns -1 after window close, in the loop check for getmessage error like msdn example
    Code:
    	// Message Loop
    	BOOL  b_Ret = FALSE;
    	while( ( b_Ret = GetMessage(&kMessage,hWindow,0,0) )!= FALSE )
    	{
    		if (b_Ret == -1){
    
    			break;
    		}
    		TranslateMessage(&kMessage);
    		DispatchMessage(&kMessage);
    	}
    yes it is fine to replace with NULL, gets messages for all windows. placing the window handle in has caused the call to fail after the window is destroyed... because the hWindow is invalid and cannot get messages for it.
    Last edited by pYro; 05-14-2011 at 06:47 AM.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Yah... Thanks pYro.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    23
    no worries , stumped me too for a bit; so was good to find a solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Q to quit help
    By macman in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2005, 03:30 PM
  2. I quit!
    By Luigi in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2002, 08:30 AM
  3. Are Your Sure You Want To Quit? Help Please
    By paulroseby in forum C Programming
    Replies: 4
    Last Post: 10-23-2002, 04:34 PM
  4. Last try, then i quit...
    By Silentsharp in forum C++ Programming
    Replies: 15
    Last Post: 04-06-2002, 10:06 PM

Tags for this Thread


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