The following code runs well and very fast on Windows 7/8/10.
It just gets the root element and root element's first child element by UIAutomation COM API.
But after updating to Windows 11, the API IUIAutomationTreeWalker.GetFirstChildElement blocks and costs 60 seconds!
IUIAutomationTreeWalker.GetFirstChildElement only costs 0 ms on Windows 10.
#include <iostream>
#include <UIAutomation.h>
#include <locale.h>
int main()
{
setlocale(LC_ALL, "");
CoInitialize(nullptr);
IUIAutomation* pAutomation = nullptr;
IUIAutomationTreeWalker* pRawTreeWalker = nullptr;
if (!pAutomation)
{
HRESULT hr = CoCreateInstance(__uuidof(CUIAutomation), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IUIAutomation), reinterpret_cast<void**>(&pAutomation));
printf("AutomationClient: CoCreateInstance CUIAutomation %s.\n", SUCCEEDED(hr) ? "succeed" : "failed");
}
if (pAutomation)
{
pAutomation->get_RawViewWalker(&pRawTreeWalker);
IUIAutomationElement* root = nullptr;
HRESULT hr = pAutomation->GetRootElement(&root);
BSTR name, className;
hr = root->get_CurrentName(&name);
hr = root->get_CurrentClassName(&className);
wprintf(L"root element: %s %s\n", className, name);
SysFreeString(name);
SysFreeString(className);
DWORD startTick = GetTickCount();
IUIAutomationElement* child = nullptr;
hr = pRawTreeWalker->GetFirstChildElement(root, &child);
DWORD cost = GetTickCount() - startTick;
hr = root->get_CurrentName(&name);
hr = root->get_CurrentClassName(&className);
wprintf(L"get root element first child, cost %u ms, child: %s %s\n", cost, className, name);
SysFreeString(name);
SysFreeString(className);
root->Release();
root = nullptr;
child->Release();
child = nullptr;
}
if (pRawTreeWalker)
{
pRawTreeWalker->Release();
pRawTreeWalker = nullptr;
}
if (pAutomation)
{
pAutomation->Release();
pAutomation = nullptr;
}
CoUninitialize();
std::cout << "EXIT!\n";
}
Windows 10
Windows 11
I also post the bug here: https://techcommunity.microsoft.com/t5/report-an-issue/windows-11-uiautomation-com-api-iuiautomationtreewalker/m-p/3005421