|
| 1 | +#include <uiAutomationClient.h> |
| 2 | +#include <winrt/windows.foundation.h> |
| 3 | +#include <winrt/windows.foundation.collections.h> |
| 4 | +#include <winrt/windows.ui.uiautomation.core.h> |
| 5 | + |
| 6 | +using namespace winrt::Windows::Foundation; |
| 7 | +using namespace winrt::Windows::UI::UIAutomation; |
| 8 | +using namespace winrt::Windows::UI::UIAutomation::Core; |
| 9 | + |
| 10 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOp_create(void** ppRemoteOp) { |
| 11 | + *ppRemoteOp = winrt::detach_abi(CoreAutomationRemoteOperation()); |
| 12 | + return S_OK; |
| 13 | +} |
| 14 | + |
| 15 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOp_free(void* arg_pRemoteOp) { |
| 16 | + CoreAutomationRemoteOperation operation {nullptr}; |
| 17 | + winrt::attach_abi(operation, arg_pRemoteOp); |
| 18 | + return S_OK; |
| 19 | +} |
| 20 | + |
| 21 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOp_importElement(void* arg_pRemoteOp, int arg_registerID, IUIAutomationElement* arg_pElement) { |
| 22 | + CoreAutomationRemoteOperation operation {nullptr}; |
| 23 | + winrt::copy_from_abi(operation, arg_pRemoteOp); |
| 24 | + if(!operation) { |
| 25 | + return E_FAIL; |
| 26 | + } |
| 27 | + AutomationElement element {nullptr}; |
| 28 | + winrt::copy_from_abi(element, arg_pElement); |
| 29 | + if(!element) { |
| 30 | + return E_FAIL; |
| 31 | + } |
| 32 | + operation.ImportElement(AutomationRemoteOperationOperandId{arg_registerID}, element); |
| 33 | + return S_OK; |
| 34 | +} |
| 35 | + |
| 36 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOp_importTextRange(void* arg_pRemoteOp, int arg_registerID, IUIAutomationTextRange* arg_pTextRange) { |
| 37 | + CoreAutomationRemoteOperation operation {nullptr}; |
| 38 | + winrt::copy_from_abi(operation, arg_pRemoteOp); |
| 39 | + if(!operation) { |
| 40 | + return E_FAIL; |
| 41 | + } |
| 42 | + AutomationTextRange textRange {nullptr}; |
| 43 | + winrt::copy_from_abi(textRange, arg_pTextRange); |
| 44 | + if(!textRange) { |
| 45 | + return E_FAIL; |
| 46 | + } |
| 47 | + operation.ImportTextRange(AutomationRemoteOperationOperandId{arg_registerID}, textRange); |
| 48 | + return S_OK; |
| 49 | +} |
| 50 | + |
| 51 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOp_addToResults(void* arg_pRemoteOp, int arg_registerID) { |
| 52 | + CoreAutomationRemoteOperation operation {nullptr}; |
| 53 | + winrt::copy_from_abi(operation, arg_pRemoteOp); |
| 54 | + if(!operation) { |
| 55 | + return E_FAIL; |
| 56 | + } |
| 57 | + operation.AddToResults(AutomationRemoteOperationOperandId{arg_registerID}); |
| 58 | + return S_OK; |
| 59 | +} |
| 60 | + |
| 61 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOp_isOpcodeSupported(void* arg_pRemoteOp, uint32_t arg_opcode, bool* pIsSupported) { |
| 62 | + CoreAutomationRemoteOperation operation {nullptr}; |
| 63 | + winrt::copy_from_abi(operation, arg_pRemoteOp); |
| 64 | + if(!operation) { |
| 65 | + return E_FAIL; |
| 66 | + } |
| 67 | + *pIsSupported = operation.IsOpcodeSupported(arg_opcode); |
| 68 | + return S_OK; |
| 69 | +} |
| 70 | + |
| 71 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOp_execute(void* arg_pRemoteOp, uint8_t arg_byteCodeBuffer[], int arg_byteCodeBufferLength, void** ppResults) { |
| 72 | + CoreAutomationRemoteOperation operation {nullptr}; |
| 73 | + winrt::copy_from_abi(operation, arg_pRemoteOp); |
| 74 | + if(!operation) { |
| 75 | + return E_FAIL; |
| 76 | + } |
| 77 | + auto results = operation.Execute(winrt::array_view<uint8_t>(arg_byteCodeBuffer, arg_byteCodeBufferLength)); |
| 78 | + *ppResults = winrt::detach_abi(results); |
| 79 | + return S_OK; |
| 80 | +} |
| 81 | + |
| 82 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOpResult_getErrorLocation(void* arg_pResults, int* pErrorLocation) { |
| 83 | + AutomationRemoteOperationResult results {nullptr}; |
| 84 | + winrt::copy_from_abi(results, arg_pResults); |
| 85 | + if(!results) { |
| 86 | + return E_INVALIDARG; |
| 87 | + } |
| 88 | + *pErrorLocation = results.ErrorLocation(); |
| 89 | + return S_OK; |
| 90 | +} |
| 91 | + |
| 92 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOpResult_getExtendedError(void* arg_pResults, HRESULT* pExtendedError) { |
| 93 | + AutomationRemoteOperationResult results {nullptr}; |
| 94 | + winrt::copy_from_abi(results, arg_pResults); |
| 95 | + if(!results) { |
| 96 | + return E_INVALIDARG; |
| 97 | + } |
| 98 | + *pExtendedError = results.ExtendedError(); |
| 99 | + return S_OK; |
| 100 | +} |
| 101 | + |
| 102 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOpResult_getStatus(void* arg_pResults, int* pStatus) { |
| 103 | + AutomationRemoteOperationResult results {nullptr}; |
| 104 | + winrt::copy_from_abi(results, arg_pResults); |
| 105 | + if(!results) { |
| 106 | + return E_INVALIDARG; |
| 107 | + } |
| 108 | + auto status = results.Status(); |
| 109 | + *pStatus = static_cast<int>(status); |
| 110 | + return S_OK; |
| 111 | +} |
| 112 | + |
| 113 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOpResult_hasOperand(void* arg_pResults, int arg_registerID, bool* pHasOperand) { |
| 114 | + AutomationRemoteOperationResult results {nullptr}; |
| 115 | + winrt::copy_from_abi(results, arg_pResults); |
| 116 | + if(!results) { |
| 117 | + return E_INVALIDARG; |
| 118 | + } |
| 119 | + *pHasOperand = results.HasOperand(AutomationRemoteOperationOperandId{arg_registerID}); |
| 120 | + return S_OK; |
| 121 | +} |
| 122 | + |
| 123 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOpResult_free(void* arg_pResults) { |
| 124 | + AutomationRemoteOperationResult results {nullptr}; |
| 125 | + winrt::attach_abi(results, arg_pResults); |
| 126 | + return S_OK; |
| 127 | +} |
| 128 | + |
| 129 | +HRESULT IInspectableToVariant(winrt::Windows::Foundation::IInspectable result, VARIANT* arg_pVariant) { |
| 130 | + if(!result) { |
| 131 | + // operand is NULL. |
| 132 | + return S_OK; |
| 133 | + } |
| 134 | + auto propVal = result.try_as<IPropertyValue>(); |
| 135 | + if(propVal) { |
| 136 | + // Unbox property value into VARIANT |
| 137 | + auto propType = propVal.Type(); |
| 138 | + switch(propVal.Type()) { |
| 139 | + case PropertyType::Int32: |
| 140 | + arg_pVariant->vt = VT_I4; |
| 141 | + arg_pVariant->lVal = propVal.GetInt32(); |
| 142 | + break; |
| 143 | + case PropertyType::String: |
| 144 | + arg_pVariant->vt = VT_BSTR; |
| 145 | + arg_pVariant->bstrVal = SysAllocString(propVal.GetString().c_str()); |
| 146 | + break; |
| 147 | + case PropertyType::Boolean: |
| 148 | + arg_pVariant->vt = VT_BOOL; |
| 149 | + arg_pVariant->boolVal = propVal.GetBoolean() ? VARIANT_TRUE : VARIANT_FALSE; |
| 150 | + break; |
| 151 | + case PropertyType::Inspectable: |
| 152 | + arg_pVariant->vt = VT_UNKNOWN; |
| 153 | + arg_pVariant->punkVal = static_cast<::IUnknown*>(winrt::detach_abi(propVal.as<winrt::Windows::Foundation::IUnknown>())); |
| 154 | + break; |
| 155 | + default: |
| 156 | + return E_NOTIMPL; |
| 157 | + } |
| 158 | + return S_OK; |
| 159 | + } |
| 160 | + auto vec = result.try_as<winrt::Windows::Foundation::Collections::IVector<winrt::Windows::Foundation::IInspectable>>(); |
| 161 | + if(vec) { |
| 162 | + // Unbox vector into VARIANT array. |
| 163 | + auto vecSize = vec.Size(); |
| 164 | + arg_pVariant->vt = VT_ARRAY | VT_VARIANT; |
| 165 | + arg_pVariant->parray = SafeArrayCreateVector(VT_VARIANT, 0, vecSize); |
| 166 | + if(!arg_pVariant->parray) { |
| 167 | + return E_OUTOFMEMORY; |
| 168 | + } |
| 169 | + for(ULONG i = 0; i < vecSize; i++) { |
| 170 | + auto vecItem = vec.GetAt(i); |
| 171 | + auto hr = IInspectableToVariant(vecItem, &static_cast<VARIANT*>(arg_pVariant->parray->pvData)[i]); |
| 172 | + if(FAILED(hr)) { |
| 173 | + return hr; |
| 174 | + } |
| 175 | + } |
| 176 | + return S_OK; |
| 177 | + } |
| 178 | + // Just treat it as an IUnknown. |
| 179 | + arg_pVariant->vt = VT_UNKNOWN; |
| 180 | + arg_pVariant->punkVal = static_cast<::IUnknown*>(winrt::detach_abi(result.as<winrt::Windows::Foundation::IUnknown>())); |
| 181 | + return S_OK; |
| 182 | +} |
| 183 | + |
| 184 | +extern "C" __declspec(dllexport) HRESULT __stdcall remoteOpResult_getOperand(void* arg_pResults, int arg_registerID, VARIANT* arg_pVariant) { |
| 185 | + AutomationRemoteOperationResult results {nullptr}; |
| 186 | + winrt::copy_from_abi(results, arg_pResults); |
| 187 | + if(!results) { |
| 188 | + return E_INVALIDARG; |
| 189 | + } |
| 190 | + auto result = results.GetOperand(AutomationRemoteOperationOperandId{arg_registerID}); |
| 191 | + return IInspectableToVariant(result, arg_pVariant); |
| 192 | +} |
0 commit comments