如何使用 UIautomation 和纯 C 从浏览器检索 URL

时间:2021-01-07 14:11:34

标签: c com ui-automation

我想获取浏览器 Internet Explorer 的 URL。我在 C++ 中找到了以下代码 (Get active Tab URL in Chrome with C++),我正在尝试将其转换为纯 C。

IUIAutomationCondition *pCondition = NULL;
VARIANT url;
VARIANT varProp;
varProp.vt = VT_I4;
varProp.lVal = UIA_EditControlTypeId;
hr = IUIAutomation_CreatePropertyCondition(pAutomation,UIA_ControlTypePropertyId,varProp,&pCondition);
if(SUCCEEDED(hr)){
  hr = IUIAutomationElement_FindFirst(pNode,TreeScope_Descendants,pCondition,&urlBar);
  if(urlBar!= NULL){
    IUIAutomationElement_GetCurrentPropertyValue(urlBar,UIA_ValueValuePropertyId,&url);
    MessageBox(0, url.bstrVal, L"success", 0);
  }
}
IUnknown_Release(pCondition);

当我想从浏览器检索值时,此代码会在消息框中打印文本 URL“h”,该值应为 www.amazon.com。我不知道我做错了什么。如果有人可以帮助我,我真的很感激。

enter image description here

1 个答案:

答案 0 :(得分:0)

变量 url.bstrVal 是 BSTR,那么我应该使用 MessageBoxW。使用 MessageBox,代码只打印 url 的第一个字母。使用 MessageBoxW,我可以打印整个 URL。

相关问题