Sign in to follow this  
johnnyBravo

Problems after getting WNDPROC messages through non static function in window class..

Recommended Posts

Hi, I've made a Window class, eg.
class CWindow {
private:
	static CWindow* cWindow;
	static LRESULT WINAPI wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
		return cWindow->msgProc(hWnd, msg, wParam, lParam); //fixed
	}
	HWND hwnd;
public:
	LRESULT Main::msgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
		switch(msg) {
			case WM_CHAR:
				if(VK_ESCAPE == wParam) {
					PostQuitMessage(0);
					return 0;
				}

				break;

			case WM_DESTROY:
				PostQuitMessage(0);
				return 0;
		}

		return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	CWindow() {
		cWindow = this;
	}	
	
};


When I call this code below in the msgProc function which is returned from the static WndProc function: case WM_CHAR: if(VK_ESCAPE == wParam) { PostQuitMessage(0); return 0; } break; The PostQuitMessge it causes a
Quote:
Unhandled exception at 0x0045ab77 in Life.exe: 0xC00000FD: Stack overflow.
But when I call that in the normal static WndProc it doesn't crash at all, and runs fine. So I believe it must be something to do with me returning the non static msgProc inside the WndProc. Any ideas on why it crashes like that? Thanks [Edited by - johnnyBravo on December 6, 2004 3:21:49 AM]

Share this post


Link to post
Share on other sites

static LRESULT WINAPI wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
return msgProc(hWnd, msg, wParam, lParam);
}


I don't see how this code should compile if msgProc is not static...?

The way I go about it is using a hashtable for the hWnd value that then points to the appropriate Window class... My code is almost exactly:


static LRESULT WINAPI wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
return table[hWnd]->msgProc(msg, wParam, lParam);
}


where table is the hash table of Window pointers.



does the stack overflow occur imediatly after the return after PostQuitMessage? or are more window messages fired off because of PostQuitMessage?

Share this post


Link to post
Share on other sites
because of the postquitmessage is fired.

what I do is have a static CWindow value stored in the CWindow class.

Then I make the static CWindow variable equal the current instance of the class, so I can then call msgProc from that instance.


Hey with your code, could you try put this in it, and see if it works:

switch(msg) {
case WM_CHAR:
if(VK_ESCAPE == wParam) {
PostQuitMessage(0);
return 0;
}
break;
}



Thanks

Share this post


Link to post
Share on other sites
As RipTorn mentioned, this code shouldn't compile:

static LRESULT WINAPI wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
return msgProc(hWnd, msg, wParam, lParam);
}


Perhaps you meant to do:

static LRESULT WINAPI wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
return cWindow->msgProc(hWnd, msg, wParam, lParam);
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
call PostQuitMessage anywhere other than in response to a WM_DESTROY msg is not a good idea since you haven't yet closed down your application window(s) and you're telling the OS that the app wants to quit. try calling DestroyWindow in response to the ESCAPE key instead.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this