diff --git a/apps/desktop/src/platform/windows/desktop_automation.cpp b/apps/desktop/src/platform/windows/desktop_automation.cpp index 321e18cc..3585bac8 100644 --- a/apps/desktop/src/platform/windows/desktop_automation.cpp +++ b/apps/desktop/src/platform/windows/desktop_automation.cpp @@ -6,12 +6,14 @@ #include #include #include +#include #include namespace crossdesk { namespace { using nlohmann::json; using Microsoft::WRL::ComPtr; +using Bstr = std::unique_ptr; std::string Utf8(BSTR value) { if (!value) return {}; const int length = std::min(int(SysStringLen(value)), 256); @@ -64,7 +66,14 @@ json Perform(ComPtr element, const std::string& operation, } } else if (operation == "set_value") { ComPtr pattern; - if (SUCCEEDED(element->GetCurrentPatternAs(UIA_ValuePatternId, IID_PPV_ARGS(&pattern)))) result = pattern->SetValue(Wide(value).c_str()); + if (SUCCEEDED(element->GetCurrentPatternAs(UIA_ValuePatternId, IID_PPV_ARGS(&pattern)))) { + const auto text = Wide(value); + Bstr automation_value( + SysAllocStringLen(text.data(), static_cast(text.size())), + SysFreeString); + if (!automation_value) throw std::runtime_error("UI_ACTION_FAILED"); + result = pattern->SetValue(automation_value.get()); + } } if (FAILED(result)) throw std::runtime_error(result == UIA_E_ELEMENTNOTAVAILABLE ? "STALE_ELEMENT" : "UI_ACTION_FAILED"); return {{"accepted", true}};