首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAutomation GetTopLevelWindowByName

UIAutomation GetTopLevelWindowByName
EN

Stack Overflow用户
提问于 2021-09-06 06:16:19
回答 1查看 98关注 0票数 1

尝试按照下面的示例进行操作:

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

代码语言:javascript
复制
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->GetRootElement(&pRoot);这一行崩溃了

出现以下错误:

代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-06 06:39:59

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

代码语言:javascript
复制
CoCreateInstance(
    __uuidof(CUIAutomation),
    NULL,
    CLSCTX_ALL,
    _uuidof(IUIAutomation),
    (void**)&g_pAutomation);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69069869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档