diff --git a/src/modules/keyboardmanager/common/Helpers.cpp b/src/modules/keyboardmanager/common/Helpers.cpp index cb96c8030f..fc30e889be 100644 --- a/src/modules/keyboardmanager/common/Helpers.cpp +++ b/src/modules/keyboardmanager/common/Helpers.cpp @@ -428,20 +428,20 @@ namespace Helpers { while (true) { - std::wstring text; - { - std::unique_lock lock(s_queueMutex); - s_queueCV.wait(lock, [] { return !s_clipboardQueue.empty() || s_shutdown.load(); }); - if (s_shutdown.load()) - { - break; - } - text = std::move(s_clipboardQueue.front()); - s_clipboardQueue.pop(); - } - try { + std::wstring text; + { + std::unique_lock lock(s_queueMutex); + s_queueCV.wait(lock, [] { return !s_clipboardQueue.empty() || s_shutdown.load(); }); + if (s_shutdown.load()) + { + break; + } + text = std::move(s_clipboardQueue.front()); + s_clipboardQueue.pop(); + } + // Snapshot current clipboard state bool hadOriginalText = false; std::wstring originalClipboardText; @@ -490,22 +490,15 @@ namespace Helpers } } } - catch (const std::exception& ex) - { - OutputDebugStringA("KBM ClipboardWorker exception: "); - OutputDebugStringA(ex.what()); - OutputDebugStringA("\n"); - } catch (...) { - OutputDebugStringA("KBM ClipboardWorker unknown exception\n"); + OutputDebugStringA("KBM ClipboardWorker exception caught, continuing\n"); } } } - // Function to send text via clipboard paste (Ctrl+V). - // Saves the previous clipboard content and restores it asynchronously. - bool SendTextViaClipboard(const std::wstring& text) + // Inner implementation that may throw C++ exceptions. + static bool SendTextViaClipboardImpl(const std::wstring& text) { // Lazily start the worker on first use. std::call_once(s_workerInitFlag, [] { @@ -524,7 +517,7 @@ namespace Helpers }); // Enqueue the text and return immediately so we never block the - // low-level keyboard hook (WH_KEYBOARD_LL). + // low-level keyboard hook (WH_KEYBOARD_LL). { std::lock_guard lock(s_queueMutex); if (!s_clipboardQueue.empty()) @@ -538,6 +531,24 @@ namespace Helpers return true; } + // Function to send text via clipboard paste (Ctrl+V). + // Saves the previous clipboard content and restores it asynchronously. + // This function MUST NOT throw — it's called from noexcept hook handlers. + // We use __try/__except (SEH) because C++ try/catch may be optimized away + // in Release builds when the caller is noexcept. + bool SendTextViaClipboard(const std::wstring& text) noexcept + { + __try + { + return SendTextViaClipboardImpl(text); + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + OutputDebugStringA("KBM SendTextViaClipboard SEH exception caught\n"); + return false; + } + } + // Function to filter the key codes for artificial key codes int32_t FilterArtificialKeys(const int32_t& key) { diff --git a/src/modules/keyboardmanager/common/Helpers.h b/src/modules/keyboardmanager/common/Helpers.h index db3cbf3eb1..069d651397 100644 --- a/src/modules/keyboardmanager/common/Helpers.h +++ b/src/modules/keyboardmanager/common/Helpers.h @@ -42,7 +42,7 @@ namespace Helpers void SetTextKeyEvents(std::vector& keyEventArray, const std::wstring& remapping); // Function to send text via clipboard paste (Ctrl+V). Saves and restores previous clipboard content. - bool SendTextViaClipboard(const std::wstring& text); + bool SendTextViaClipboard(const std::wstring& text) noexcept; // Function to return window handle for a full screen UWP app HWND GetFullscreenUWPWindowHandle();