C++ 使用C+;在FireFox中获取活动选项卡URL+;

C++ 使用C+;在FireFox中获取活动选项卡URL+;,c++,winapi,geturl,C++,Winapi,Geturl,我尝试使用UI自动化从Firefox获取URL,但一直失败 它在铬上工作得很好。但它在Firefox中不起作用。 我认为搜索或输入地址与Chrome中的“地址和搜索栏”相同 #include <Windows.h> #include <AtlBase.h> #include <AtlCom.h> #include <UIAutomation.h> #include <stdlib.h> #define UNICODE int main

我尝试使用UI自动化从Firefox获取URL,但一直失败

它在铬上工作得很好。但它在Firefox中不起作用。 我认为搜索或输入地址与Chrome中的“地址和搜索栏”相同

#include <Windows.h>
#include <AtlBase.h>
#include <AtlCom.h>
#include <UIAutomation.h>
#include <stdlib.h>
#define UNICODE

int main()
{
    CoInitialize(NULL);
    HWND hwnd = NULL;
    while (true)
    {
        hwnd = FindWindowEx(0, hwnd, L"MozillaWindowClass", NULL);
        if (!hwnd)
            break;
        if (!IsWindowVisible(hwnd))
            continue;

        CComQIPtr<IUIAutomation> uia;
        if (FAILED(uia.CoCreateInstance(CLSID_CUIAutomation)) || !uia)
            break;

        CComPtr<IUIAutomationElement> root;
        if (FAILED(uia->ElementFromHandle(hwnd, &root)) || !root)
            break;

        CComPtr<IUIAutomationCondition> condition;


        uia->CreatePropertyCondition(UIA_ControlTypePropertyId,
            CComVariant(0xC354), &condition);


        CComPtr<IUIAutomationElement> edit;
        if (FAILED(root->FindFirst(TreeScope_Descendants, condition, &edit))
            || !edit)
            continue; //maybe we don't have the right tab, continue...

        CComVariant url;
        edit->GetCurrentPropertyValue(UIA_ValueValuePropertyId, &url);
        MessageBox(0, url.bstrVal, 0, 0);
        break;
    }
    CoUninitialize();
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义UNICODE
int main()
{
共初始化(空);
HWND HWND=NULL;
while(true)
{
hwnd=FindWindowEx(0,hwnd,L“MozillaWindowClass”,NULL);
如果(!hwnd)
打破
如果(!IsWindowVisible(hwnd))
继续;
CComQIPtr uia;
if(失败(uia.CoCreateInstance(CLSID_CUIAutomation))| |!uia)
打破
菜根;
if(失败(uia->ElementFromHandle(hwnd,&root))| |!root)
打破
买方条件;
uia->CreatePropertyCondition(uia\U控制类型属性ID,
CComVariant(0xC354),&条件;
CComPtr编辑;
if(失败(根->FindFirst(树范围\子体、条件和编辑))
||!编辑)
继续;//可能我们没有正确的选项卡,继续。。。
c可变url;
编辑->GetCurrentPropertyValue(UIA\U ValuePropertyId和url);
MessageBox(0,url.bstrVal,0,0);
打破
}
coninitialize();
返回0;
}
消息框中会显示一个空白值
我想获得活动选项卡URL

以上代码的主要部分是为使用Chrome而设计的,而不是Firefox

使用该工具检查Firefox中控件的排列。您将看到以下结构:

Firefox window
    -> "" toobar
    -> "" toobar
    -> "Navigation Toolbar" toobar
        -> "" combobox
            -> "Search with Google" edit //<- url editbox
        -> "Search" combobox //<- we don't want this
Firefox窗口
->“图巴
->“图巴
->“导航工具栏”工具栏
->“”组合框
->“使用谷歌搜索”编辑/“搜索”组合框//
bool firefox_geturl(HWND hwnd)
{
    CComQIPtr<IUIAutomation> uia;
    if(FAILED(uia.CoCreateInstance(CLSID_CUIAutomation)) || !uia)
        return false;

    CComPtr<IUIAutomationElement> element;
    if(FAILED(uia->ElementFromHandle(hwnd, &element)) || !element)
        return false;

    //initialize conditions
    CComPtr<IUIAutomationCondition> toolbar_cond;
    uia->CreatePropertyCondition(UIA_ControlTypePropertyId,
        CComVariant(UIA_ToolBarControlTypeId), &toolbar_cond);

    CComPtr<IUIAutomationCondition> combobox_cond;
    uia->CreatePropertyCondition(UIA_ControlTypePropertyId, 
        CComVariant(UIA_ComboBoxControlTypeId), &combobox_cond);

    CComPtr<IUIAutomationCondition> editbox_cond;
    uia->CreatePropertyCondition(UIA_ControlTypePropertyId, 
        CComVariant(UIA_EditControlTypeId), &editbox_cond);

    //find the top toolbars
    CComPtr<IUIAutomationElementArray> toolbars;
    if(FAILED(element->FindAll(TreeScope_Children, toolbar_cond, &toolbars)) || !toolbars)
        return false;

    int toolbars_count = 0;
    toolbars->get_Length(&toolbars_count);
    for(int i = 0; i < toolbars_count; i++)
    {
        CComPtr<IUIAutomationElement> toolbar;
        if(FAILED(toolbars->GetElement(i, &toolbar)) || !toolbar)
            continue;

        //find the comboxes for each toolbar
        CComPtr<IUIAutomationElementArray> comboboxes;
        if(FAILED(toolbar->FindAll(TreeScope_Children, combobox_cond, &comboboxes)) || !comboboxes)
            return false;

        int combobox_count = 0;
        comboboxes->get_Length(&combobox_count);
        for(int j = 0; j < combobox_count; j++)
        {
            CComPtr<IUIAutomationElement> combobox;
            if(FAILED(comboboxes->GetElement(j, &combobox)) || !combobox)
                continue;

            CComVariant test;
            if(FAILED(combobox->GetCurrentPropertyValue(UIA_ValueValuePropertyId, &test)))
                continue;

            //we are interested in a combobox which has no lable
            if(wcslen(test.bstrVal))
                continue;

            //find the first editbox
            CComPtr<IUIAutomationElement> edit;
            if(FAILED(combobox->FindFirst(TreeScope_Descendants, editbox_cond, &edit)) || !edit)
                continue;

            CComVariant bstr;
            if(FAILED(edit->GetCurrentPropertyValue(UIA_ValueValuePropertyId, &bstr)))
                continue;

            MessageBoxW(0, bstr.bstrVal, L"success", 0);
            return true;
        }
    }
    return false;
}

int main()
{
    CoInitialize(NULL);
    HWND hwnd = NULL;
    while(true)
    {
        hwnd = FindWindowEx(0, hwnd, L"MozillaWindowClass", NULL);
        if(!hwnd)
            break;
        if(!IsWindowVisible(hwnd))
            continue;
        firefox_geturl(hwnd);
        break;
    }
    CoUninitialize();
    return 0;
}