Hi,
I'm using CUIAutomation to extract minor data from Chrome's tool bar (not the page itself).
At the same time, I can inspect Chrome using Accessibility Insights for Windows and it works just fine. So there should be a way to avoid this problem.
- Is it a bug?
- Are there other APIs that I can use that avoid this problem?
Environment
Chrome: 92.0.4515.159 (64 bit)
Windows: 10.0.19043 Build 19043
CPU: i7-9750H
Memory: 32.0 GB
// VS 2017 Test Code:
#include <cassert>
#include <chrono>
#include <ios>
#include <iostream>
#include <windows.h>
#include <uiautomationclient.h>
#include <comutil.h>
#ifdef _DEBUG
# pragma comment(lib, "comsuppwd.lib")
#else
# pragma comment(lib, "comsuppw.lib")
#endif
# pragma comment(lib, "wbemuuid.lib")
int main()
{
HRESULT hr = CoInitialize(NULL);
assert(SUCCEEDED(hr));
IUIAutomation *pAutomation;
hr = CoCreateInstance(
__uuidof(CUIAutomation),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IUIAutomation),
(void **)(&pAutomation)
);
assert(SUCCEEDED(hr));
std::cout << "Go to window..." << std::endl;
Sleep(2000);
HWND hWin = GetForegroundWindow();
assert(hWin != NULL);
std::cout << "Running..." << std::endl;
IUIAutomationElement *win;
hr = pAutomation->ElementFromHandle(hWin, &win);
assert(hWin != NULL);
VARIANT varCtrlL;
varCtrlL.vt = VT_BSTR;
varCtrlL.bstrVal = _com_util::ConvertStringToBSTR("Ctrl+L");
IUIAutomationCondition *ctrlLAccessKeyCondition = NULL;
hr = pAutomation->CreatePropertyCondition(UIA_AccessKeyPropertyId, varCtrlL, &ctrlLAccessKeyCondition);
assert(SUCCEEDED(hr));
IUIAutomationElement *address;
std::chrono::steady_clock::time_point before = std::chrono::steady_clock::now();
hr = win->FindFirst(
TreeScope_Descendants,
ctrlLAccessKeyCondition,
&address
);
std::chrono::steady_clock::time_point after = std::chrono::steady_clock::now();
std::cout << "Took "
<< (std::chrono::duration_cast<std::chrono::milliseconds>(after - before).count())
<< " ms"
<< std::endl;
assert(SUCCEEDED(hr));
}