try
{
while(1)
{
// your message loop
}
}
catch (...)
{
// if you want to display a messagebox here, you better give a NON-NULL handle
MessageBox(hwnd, "Exception thrown here", "Error", MB_OK);
}
For those using Messageboxes when catching exceptions: Read
I bring to your attention when using messageboxs with exceptions
If you use messagebox to display when an exception is thrown (typically in the main), the handle in the messagebox must not be NULL..
If it is NULL, it will crash if exception is thrown in the WM_MOUSEMOVE message, but not others like WM_KEYDOWN. There may be a few others but I''m not waiting to find out more..
If you are running fullscreen, you will just get an abnoral program termination messagebox and the system will crash..
Take heed..and obey.. or bang head against the wall like I did..
An example like this
June 15, 2000 07:45 AM
FYI -
I frequently use MessageBox with a -0- window handle. I think your problem might lay elsewhere.
If you are throwing an exception in the WM_MOUSEMOVE handler, it might be that you are just running out of stack space as those WM_MOUSEMOVE messages can come pretty fast.
"If you are running fullscreen.." ...are you using DirectX and trying to display a message box? If so, are you calling FlipToGDISurface() before trying to display your message box?
I never used the flip to GDI function. I have a global Window handle and use that with my messageBox functions.. it works great.
The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.
?Have a nice day!?
It doesn''t have to be fullscreen. I test it on a windowed application and it still does that..
And as I said, it doesn''t happen in other messages, only the WM_MOUSEMOVE..
Here is my test program for you guys to try. Just create a empty win32 application.
Press E to throw an exception. Note is quit cleanly.
If you move the mouse for a while ( I use a static to keep track how many times the WM_MOUSEMOVE message is received, and throw an exception after receiving it for 10 times, then an exception is thrown. See how it crashes..
#include <windows.h>//---------- Exception classclass CException{public: CException(const char *string) { strncpy(m_error_string, string, 128); } char *Get_String() { return(m_error_string); };private: char m_error_string[128];}; // end class CException//---------- WndprocLRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam){ switch(msg) { case WM_KEYDOWN: switch(wparam) { case VK_ESCAPE: PostQuitMessage(0); break; //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // the exception thrown by keyboard is ok //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% case ''E'': throw CException("Thrown by key pressed"); break; } // end switch(wparam) return(0); break; case WM_MOUSEMOVE: //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // This is the part where it dies //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% static count; if (count++ > 10) throw CException("Thrown by mouse pressed"); return(0); break; } return(DefWindowProc( hwnd, msg, wparam, lparam ));} // end wndproc//---------- win mainint WINAPI WinMain(HINSTANCE h_instance, HINSTANCE h_previ_nstance, LPSTR lp_cmd_line, int show_cmd){ // create window //---------------- WNDCLASSEX wndclass; // generic window class HWND hwnd; // window handle wndclass.cbSize = sizeof(wndclass); wndclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; //types of received message wndclass.lpfnWndProc = (WNDPROC)WndProc;// event handler wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = h_instance; wndclass.hIcon = NULL; wndclass.hCursor = NULL; // cursor wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // background wndclass.lpszMenuName = NULL; wndclass.lpszClassName = "Test"; wndclass.hIconSm = NULL; // small icon if (!RegisterClassEx(&wndclass)) // registering class return(0); hwnd = CreateWindowEx( 0, "Test", "Test", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, 640, 480, NULL, NULL, h_instance, NULL); if (!hwnd) // if error return(0); ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); SetFocus(hwnd); // the main message pump //---------------------------------------------------------------------- MSG msg; try { while(1) // the main message pump { if (PeekMessage(&msg, NULL,0,0,PM_REMOVE)) { if (WM_QUIT == msg.message) break; TranslateMessage(&msg); DispatchMessage(&msg); } else WaitMessage(); } // end while(1) } // end try catch(...) { // display the exception //---------------------- MessageBox(NULL, "exception", "Error", MB_OK | MB_TOPMOST); } return(0);} // end winmain
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement