diff options
-rw-r--r-- | c/main.cpp | 25 |
1 files changed, 24 insertions, 1 deletions
@@ -23,6 +23,7 @@ int g_bViewWatched = 1; int g_bThread = 0; int g_iDPI = 96; static int g_cxVScroll; +void OnTerminate(void); static LRESULT CALLBACK CBTProc(int, WPARAM, LPARAM); static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); static INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM); @@ -34,6 +35,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, { LPTSTR tszErr; + /* Exit gracefully on uncaught exception. */ + std::set_terminate(OnTerminate); + /* Set constant values. */ if (auto upLib = try_make<Library>(TEXT("uxtheme.dll"))) if (upLib->GetProcAddress("SetWindowTheme")) { @@ -123,11 +127,30 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PL_halt(0); return 0; -f: MessageBox(NULL, tszErr, TEXT("Error"), MB_ICONERROR); +f: MessageBox(NULL, tszErr, TEXT("Fatal Error"), MB_ICONERROR); PL_halt(1); return 1; } +void OnTerminate() +{ + try { + std::rethrow_exception(std::current_exception()); + } catch (term_t &t) { + char *sz; + TCHAR *tsz; + if (PL_get_chars(t, &sz, CVT_WRITE)) /* TODO: PL_get_wchars */ + tsz = TszFromSz(sz, CP_UTF8); + else + tsz = TEXT("The program was terminated due to an exception."); + MessageBox(NULL, tsz, TEXT("Fatal Error"), MB_ICONERROR); + } catch (...) { + MessageBox(NULL, TEXT("The program was terminated due to an exception."), + TEXT("Fatal Error"), MB_ICONERROR); + } + std::_Exit(1); +} + static LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode != HCBT_CREATEWND) return 0; |