From b2d939011f99eb9756acbc34fefafb9970f37762 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 14 Aug 2019 15:08:16 +0200 Subject: [PATCH] Tolerate already registered monitor master and slave window classes Signed-off-by: Simon Rozman --- lib/EAPBase_UI/src/Module.cpp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/EAPBase_UI/src/Module.cpp b/lib/EAPBase_UI/src/Module.cpp index 25a6905..9f13785 100644 --- a/lib/EAPBase_UI/src/Module.cpp +++ b/lib/EAPBase_UI/src/Module.cpp @@ -54,11 +54,16 @@ eap::monitor_ui::monitor_ui(_In_ HINSTANCE module, _In_ const GUID &guid) : _T(__FUNCTION__), // lpszClassName NULL // hIconSm }; - ATOM wnd_class = RegisterClassEx(&wnd_class_desc_master); - if (!wnd_class) - throw win_runtime_error(__FUNCTION__ " Error registering master monitor window class."); + LPCTSTR wnd_class = reinterpret_cast(RegisterClassEx(&wnd_class_desc_master)); + if (!wnd_class) { + DWORD dwResult = GetLastError(); + if (dwResult == ERROR_CLASS_ALREADY_EXISTS) + wnd_class = _T(__FUNCTION__); + else + throw win_runtime_error(dwResult, __FUNCTION__ " Error registering master monitor window class."); + } tstring_guid guid_str(guid); - HWND hwnd_master = FindWindowEx(HWND_MESSAGE, NULL, reinterpret_cast(wnd_class), guid_str.c_str()); + HWND hwnd_master = FindWindowEx(HWND_MESSAGE, NULL, wnd_class, guid_str.c_str()); if (hwnd_master) { // Another monitor is already running. m_is_master = false; @@ -78,9 +83,14 @@ eap::monitor_ui::monitor_ui(_In_ HINSTANCE module, _In_ const GUID &guid) : _T(__FUNCTION__) _T("-Slave"), // lpszClassName NULL // hIconSm }; - wnd_class = RegisterClassEx(&wnd_class_desc_slave); - if (!wnd_class) - throw win_runtime_error(__FUNCTION__ " Error registering slave monitor window class."); + wnd_class = reinterpret_cast(RegisterClassEx(&wnd_class_desc_slave)); + if (!wnd_class) { + DWORD dwResult = GetLastError(); + if (dwResult == ERROR_CLASS_ALREADY_EXISTS) + wnd_class = _T(__FUNCTION__) _T("-Slave"); + else + throw win_runtime_error(dwResult, __FUNCTION__ " Error registering slave monitor window class."); + } } else { // This is a fresh monitor. m_is_master = true;