UIAutomation COM API IUIAutomationTreeWalker.GetFirstChildElement costs 60 seconds on Windows 11

kaisheng yin 6 Reputation points
2021-11-25T12:46:47.873+00:00

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
medium

Windows 11
medium

I also post the bug here: https://techcommunity.microsoft.com/t5/report-an-issue/windows-11-uiautomation-com-api-iuiautomationtreewalker/m-p/3005421

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,422 questions
{count} votes

1 answer

Sort by: Most helpful
  1. kaisheng yin 6 Reputation points
    2021-12-08T03:07:00.927+00:00

    I clicked https://developer.microsoft.com/en-us/windows/support/,
    but I can't submit an incident for Windows SDK.

    It says that
    We were unable to find an eligible support plan associated with your account. Please add a support plan from the options below.

    Unable to retrieve purchase options for the current region.
    155802-2021-12-08-110536.png

    1 person found this answer helpful.
    0 comments No comments