Skip to main content
gamedev.net
🔒 Locked

[C++] Unhandled exception with win32 dialogs

Started by mind in a box 3 replies 1k views
Original Post
mind in a box
mind in a box
Hi all!

First of all: I'm a bit tired right now, so maybe I just missed something in my code.

I wrote a little wrapper for win32 dialogs that gives me the ability to put the MsgProc into my class instead of having a global function.

Unfortunately it crashes when I want to access my object, with an unhandled exception.

Here is the code:
/** This function creates the dialog window */HWND wtechDialog::CreateDialogWindow(HINSTANCE hInstance, LPWSTR Dialog){	// Create the window with this object as param	DialogWindow = CreateDialogParam(hInstance, Dialog, NULL, (DLGPROC)StaticWndProc, (LPARAM) this);	hInst=hInstance;	return DialogWindow;}/** You can use this message to process the messages */LRESULT wtechDialog::WndProc(HWND hDlg, int Msg, WPARAM wParam, LPARAM lParam){	return 0;}/** This is used internally */LRESULT wtechDialog::StaticWndProc(HWND hDlg, int Msg, WPARAM wParam, LPARAM lParam){	if (Msg == WM_INITDIALOG)	{		LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;		wtechDialog *Dialog = (wtechDialog *)pcs->lpCreateParams;		::SetWindowLongPtrW(			hDlg,			GWLP_USERDATA,			PtrToUlong(Dialog)			);		//Call the user msg proc		return Dialog->WndProc(hDlg, Msg, wParam, lParam); //<------ Crashes here!	}	else	{		wtechDialog *Dialog = reinterpret_cast<wtechDialog *>(static_cast<LONG_PTR>(			::GetWindowLongPtrW(			hDlg,			GWLP_USERDATA			)));		if(Dialog)		{			//Call the user msg proc			return Dialog->WndProc(hDlg, Msg, wParam, lParam); //<----- If I comment the above out, it crashes here!		}	}}


How can that be? I did this several times before, and this code was mostly copied out from my other files.
empirical2
empirical2
Where is the instance of wtechDialog being created? Is it global? Do you use new? Could it be that it is being deleted or falling out of scope?
empirical2
empirical2
In addition the first thing you need to do is break on
DialogWindow = CreateDialogParam(hInstance, Dialog, NULL, (DLGPROC)StaticWndProc, (LPARAM) this);

Write down the address of 'this'.

Then move forward to the crash point. Does the address of Dialog match what you wrote down?

If it is then the object is being deleted or there is some corruption going on. If not there is some failure when passing this address around windows.
mind in a box
mind in a box
Quote:

Write down the address of 'this'.

Then move forward to the crash point. Does the address of Dialog match what you wrote down?


Ah.. I forgot to check that. The address is indeed different. But I also found out where the right address was gone:

if (Msg == WM_INITDIALOG)	{		LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;		wtechDialog *Dialog = (wtechDialog *)pcs->lpCreateParams;...


Has to become:
if (Msg == WM_INITDIALOG)	{		wtechDialog *Dialog = (wtechDialog *)lParam;...

And the address is right again.
Maybe it was the fact that I copied the code from a wrapper for non-dialog windows which may need that LPCREATESTRUCT. Or it is because I'm using WM_INITDIALOG here instead of WM_CREATE.

Works now! [smile]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.