Wierd Win32 Problems?

This is a discussion on Wierd Win32 Problems? within the Windows Programming forums, part of the Platform Specific Boards category; yes, i know theis no commenting, and i apologize, but i wrote this in a bit of a hurry, and ...

  1. #1
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267

    Wierd Win32 Problems?

    yes, i know theis no commenting, and i apologize, but i wrote this in a bit of a hurry, and this is not really complicated its pretty straight foward Win32 code, and should compile on anything with few/no modificatons.

    the problem is when the window is destroyed i get an error every time?!! "Unable to Release Device Context", "Unable to Destroy Window"
    What am i missing? i've been over the code about a million times and cant find where i've screwed up!

    Code:
    //////////////////////////////////////////////////////////////////////
    //
    // Filename : Main.cpp
    
    #include <windows.h>
    
    HDC		hDC = NULL;
    HWND		hWnd = NULL;
    HINSTANCE         hInstance = NULL;
    
    LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
    
    HWND main_CreateWindow(HINSTANCE hInst, WNDPROC* wndproc, HDC* hDevC, 
                                                 char* ClassName, char* Title,int x, int y, int width, int height);
    bool main_DestroyWindow(HWND* hWind,HDC* hDevC,HINSTANCE* hInst,char* ClassName);
    
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,LPSTR lpCmpLine,int nShowCmd)
    {
    	MSG message;
    	bool bProgramEnded = false;
    	hInstance = hInst;
    	hWnd = main_CreateWindow(hInst,(WNDPROC*)&WndProc,&hDC,"Window","Main Window",60,26,800,600);
    	
    	while(!bProgramEnded)
    	{
    		if(PeekMessage(&message,NULL,0,0,PM_REMOVE))
    		{
    			TranslateMessage(&message);
    			DispatchMessage(&message);
    			if(message.message == WM_QUIT)
    				bProgramEnded = true;
    		}
    	}
    	
    	main_DestroyWindow(&hWnd,&hDC,&hInstance,"Window");
    	
    	return 0;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    	switch(uMsg)
    	{
    		case WM_CLOSE:
    		case WM_QUIT:
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			break;
    		case WM_SYSCOMMAND:	
    			switch (wParam)
    			{
    				case SC_SCREENSAVE:
    				case SC_MONITORPOWER:
    				return 0;
    			}
    			break;
    	}
    	return DefWindowProc(hWnd,uMsg,wParam,lParam);
    }
    
    HWND main_CreateWindow(HINSTANCE hInst, WNDPROC* wndproc, HDC* hDevC, 
                                                 char* ClassName, char* Title,int x, int y, int width, int height)
    {
    	HWND hNewWindow = NULL;
    	*hDevC = NULL;
    	
    	WNDCLASSEX wc;
    	RECT wRect;
    	wRect.top = 0;
    	wRect.left = 0;
    	wRect.bottom = height;
    	wRect.right = width;
    
    	wc.cbSize = sizeof(WNDCLASSEX);
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = NULL;
    	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    	wc.hInstance = hInst;
    	wc.lpfnWndProc = (WNDPROC) wndproc;
    	wc.lpszClassName = ClassName;
    	wc.lpszMenuName = NULL;
    	wc.hIconSm = NULL;
    	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    	
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL,"Failed to Register Window Class","Window Creation Error",MB_OK | MB_ICONERROR);
    		main_DestroyWindow(&hNewWindow,hDevC,&hInst,ClassName);
    		return false;
    	}
    	
    	AdjustWindowRectEx(&wRect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
    
    	hNewWindow = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
    						   ClassName,
    						   Title,
    						   WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
    						   60,26,
    						   wRect.right - wRect.left,
    						   wRect.bottom - wRect.top,
    						   NULL,NULL,
    						   hInst,
    						   NULL);
    						   
    	if(hNewWindow == NULL)
    	{
    		MessageBox(NULL,"Unable To Create Window","Window Creation Error",MB_OK | MB_ICONERROR);
    		main_DestroyWindow(&hNewWindow,hDevC,&hInst,ClassName);
    		return false;
    	}
    	
    	if(!(*hDevC = GetDC(hNewWindow)))
    	{
    		MessageBox(NULL,"Unable To Obtain Device Context","Window Creation Error", MB_OK | MB_ICONERROR);
    		main_DestroyWindow(&hNewWindow,hDevC,&hInst,ClassName);
    		return false;
    	}
    
    	ShowWindow(hNewWindow,SW_SHOWNORMAL);
    	SetForegroundWindow(hNewWindow);
    	SetFocus(hNewWindow);
    	
    	return hNewWindow;
    }
    
    bool main_DestroyWindow(HWND* hWind,HDC* hDevC,HINSTANCE* hInst,char* ClassName)
    {
    	if(!ReleaseDC(*hWind,*hDevC))
    	{
    		MessageBox(NULL,"Unable To Release Device Context","Window Destruction Error",MB_OK | MB_ICONERROR);
    		*hDevC = NULL;
    	}
    	
    	if(!DestroyWindow(*hWind))
    	{
    		MessageBox(NULL,"Unable To Destroy Window","Window Destruction Error",MB_OK | MB_ICONERROR);
    		*hWind = NULL;
    	}
    	
    	if(!UnregisterClass(ClassName,*hInst))
    	{
    		MessageBox(NULL,"Unable To Unregister Window Class","Window Destruction Error",MB_OK | MB_ICONERROR);
    		*hInst = NULL;
    	}
    	
    	return true;
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    The problem is that when you invoke the
    main_DestroyWindow the handle to the window is not valid
    (the system releases the handle right after the PostQuitMessage(0)) and any API call usiing that handle will fail.

    I don't see any other reason to receive such errors at runtime.
    damyan

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    ahh yes, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd fstream problems...
    By Jaken Veina in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2009, 04:44 PM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  4. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  5. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM

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