【问题标题】:UIAutomation GetTopLevelWindowByNameUIAutomation GetTopLevelWindowByName
【发布时间】:2021-09-06 06:16:19
【问题描述】:

尝试按照此处找到的示例进行操作:

https://docs.microsoft.com/en-us/windows/win32/winauto/uiauto-howto-find-ui-elements

WCHAR window_name[250] = L"tools";
IUIAutomationElement *window = GetTopLevelWindowByName(window_name);

IUIAutomationElement* GetTopLevelWindowByName(LPWSTR windowName)
{
    if (windowName == NULL)
        return NULL;

    CComPtr<IUIAutomation> g_pAutomation;

    VARIANT varProp;
    varProp.vt = VT_BSTR;
    varProp.bstrVal = SysAllocString(windowName);
    if (varProp.bstrVal == NULL)
        return NULL;

    IUIAutomationElement* pRoot        = NULL;
    IUIAutomationElement* pFound       = NULL;
    IUIAutomationCondition* pCondition = NULL;

    // Get the desktop element. 
    HRESULT hr = g_pAutomation->GetRootElement(&pRoot);
    if (FAILED(hr) || pRoot == NULL)
        goto cleanup;

    // Get a top-level element by name, such as "Program Manager"
    hr = g_pAutomation->CreatePropertyCondition(UIA_NamePropertyId, varProp, &pCondition);
    if (FAILED(hr))
        goto cleanup;

    pRoot->FindFirst(TreeScope_Children, pCondition, &pFound);

cleanup:
    if (pRoot != NULL)
        pRoot->Release();

    if (pCondition != NULL)
        pCondition->Release();

    VariantClear(&varProp);
    return pFound;
}

但应用程序在HRESULT hr = g_pAutomation-&gt;GetRootElement(&amp;pRoot); 行崩溃

出现错误:

File: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29333\atlmfc\include\atlcomcli.h
Line: 204

Expression: p!=0

我正在尝试的是,学习如何遍历给定窗口的名称、描述等元素。

【问题讨论】:

  • 你必须创建g_pAutomation,它不能为空,就像这里演示的(使用ATL)stackoverflow.com/a/69065685/403671或这样:CoCreateInstance(__uuidof(CUIAutomation), NULL, CLSCTX_ALL, _uuidof(IUIAutomation), (void**)&amp;g_pAutomation);
  • 知道了,谢谢,我如何在“元素名称”CComBSTR name; element-&gt;get_CurrentName(&amp;name); wprintf(L"name: %s\n", name); auto name2 = std::regex_replace(name.m_str, std::regex(".*\K-.*")); 中执行正则表达式替换@
  • 再问一个问题

标签: c++ winapi microsoft-ui-automation


【解决方案1】:

必须创建g_pAutomation,不能为空,例如:

CoCreateInstance(
    __uuidof(CUIAutomation),
    NULL,
    CLSCTX_ALL,
    _uuidof(IUIAutomation),
    (void**)&g_pAutomation);

【讨论】:

    猜你喜欢
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2013-01-05
    • 2014-08-12
    • 2010-11-03
    • 2013-09-20
    • 1970-01-01
    相关资源
    最近更新 更多