From 34c9abcf6674fece91c6d2750cd424f32cd5d792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Mon, 18 Jul 2022 02:03:22 +0200 Subject: Ensure that CBT hook is set and unset correctly. --- c/main.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'c') diff --git a/c/main.cpp b/c/main.cpp index 885d045..6a172e5 100644 --- a/c/main.cpp +++ b/c/main.cpp @@ -92,10 +92,13 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons if (!RegisterClassEx(&wc)) throw std::runtime_error("Could not register window class."); - /* Create window. Note that a CBT hook is used to initialize - * important global variables before any messages are sent to - * the new window. */ + /* Create window. A CBT hook is used to initialize important + * global variables before any messages are sent to the new + * window. It is vital that the hook is set up and torn down + * correctly. Note that if the unhook fails, it is useless to + * try to show an error dialog. */ const HHOOK hHook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId()); + if (!hHook) throw Win32Error(GetLastError()); const HWND hWnd = CreateWindowEx( 0, TEXT("Episode Browser"), @@ -105,6 +108,7 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons NULL, NULL, hInstance, NULL ); UnhookWindowsHookEx(hHook); + if (!hHook) _Exit(1); if (!hWnd) throw std::runtime_error("Could not create main window."); g_hWndStatus = CreateStatusBar(hWnd, hInstance); ShowWindow(hWnd, nCmdShow); @@ -142,7 +146,7 @@ void OnTerminate() MessageBoxA(NULL, "The program was terminated due to an exception.", "Fatal Error", MB_ICONERROR); } - std::_Exit(1); + _Exit(1); } static LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) -- cgit v1.2.3