mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 09:46:54 +02:00
[KBM] Fixing text replacement bug (#46069)
There is a bug now where text replacement is causing the app to crash. Wrapping those blocks in try catch so the worker stays alive.
This commit is contained in:
@@ -427,6 +427,8 @@ namespace Helpers
|
||||
static void ClipboardWorkerLoop()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::wstring text;
|
||||
{
|
||||
@@ -440,8 +442,6 @@ namespace Helpers
|
||||
s_clipboardQueue.pop();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 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, [] {
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace Helpers
|
||||
void SetTextKeyEvents(std::vector<INPUT>& 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();
|
||||
|
||||
Reference in New Issue
Block a user