Win32 hwnd, try catch and how to make that nice...

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
anirul
Posts: 30
Joined: Wed Oct 07, 2009 6:03 am
Location: Geneva
Contact:

Win32 hwnd, try catch and how to make that nice...

Post by anirul »

My problem occurs when I try to throw an exception under Windows, this make the whole thing crashed in debug & release mode! As under Linux everything work fine (this also depend on the exception). basically I have a try - catch around the main body of my main function and on the catch I open a MessageBox.

Code: Select all


int main(int ac, char** av) {
	try {
		// TODO add resolution chooser here
		irr::IrrlichtDevice* pdevice = irr::createDevice(
#ifndef WIN32
			irr::video::EDT_OPENGL, 
#else
			irr::video::EDT_DIRECT3D9,
#endif
			irr::core::dimension2d<unsigned int>(1024, 768),
			32,
			false,
			false,
			true);
		if (!pdevice) throw std::runtime_error("ERROR : Could not create device");
		pdevice->setWindowCaption(L"Biolite - Irrlicht Version");
		irrwin main_logic(pdevice);
		while (main_logic.runOnce(pdevice)) 
#ifndef WIN32
			usleep(1);
#else
			Sleep(1);
#endif
	} catch (std::runtime_error ex) {
		std::cerr << ex.what() << std::endl;
		// TODO change this for a more GUI oriented way
		// probaly have to make a Linux and a Mac OS X version...
#ifdef WIN32
		MessageBoxA(NULL, ex.what(), "Exception", MB_ICONEXCLAMATION);
#endif
	}
	return 0;
}
A friend of mine had a look and was able to solve the problem by adding

Code: Select all

#ifdef WIN32 
	HWND hwnd; 
	pdevice->setWindowCaption(L"ERROR"); 
	hwnd = FindWindow(NULL,TEXT("ERROR")); 
	MessageBoxA(hwnd, ex.what(), "Exception", MB_ICONEXCLAMATION); 
#endif 
Is there any ways to get the hwnd in a more elegant way? Per example from the device? I could probably also try to make theses exception lifetime a little longer and then not having this problem but this would not be really cleaner. I'm not on Windows so this is quite annoying to test for me so if this is really evident or anything forgive my question!
anirul
Posts: 30
Joined: Wed Oct 07, 2009 6:03 am
Location: Geneva
Contact:

Post by anirul »

hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Uhm, it seems that you could throw that exception also on the case of a failed device creation. Using that pointer for anything will lead to arbitrary crashes, but probably in no way to a correct message box.
anirul
Posts: 30
Joined: Wed Oct 07, 2009 6:03 am
Location: Geneva
Contact:

Post by anirul »

I tried and in this case it work (probably because the throw is casting a exception from a const char* and not from a construction as I do from other places).
Post Reply