UIAutomation GetTopLevelWindowByName
Posted
技术标签:
【中文标题】UIAutomation 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->GetRootElement(&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)***.com/a/69065685/403671或这样:CoCreateInstance(__uuidof(CUIAutomation), NULL, CLSCTX_ALL, _uuidof(IUIAutomation), (void**)&g_pAutomation);
知道了,谢谢,我如何在“元素名称”CComBSTR name; element->get_CurrentName(&name); wprintf(L"name: %s\n", name); auto name2 = std::regex_replace(name.m_str, std::regex(".*\K-.*"));
中执行正则表达式替换@
再问一个问题
【参考方案1】:
必须创建g_pAutomation
,不能为空,例如:
CoCreateInstance(
__uuidof(CUIAutomation),
NULL,
CLSCTX_ALL,
_uuidof(IUIAutomation),
(void**)&g_pAutomation);
【讨论】:
以上是关于UIAutomation GetTopLevelWindowByName的主要内容,如果未能解决你的问题,请参考以下文章