c++ - 如何在 C++ 中获取浏览器地址栏的内容?

标签 c++ visual-studio-2012

如果是 C++ 中的网络浏览器,我需要抓取事件窗口地址栏的内容。我已经想出了如何获取标题,并且可以获取记事本的内容,但我被浏览器卡住了。

我的目标是让这项工作适用于 IE、Chrome 和 Firefox。如果这需要不同的方法,我会让程序尝试每一个,直到一个返回数据。

这是我目前所拥有的:

    HWND foreground = GetForegroundWindow();
    HWND hwndEdit = FindWindowEx(foreground, NULL, "EDIT", NULL);

    const int bufferSize = 5024;
    char textBuffer[bufferSize] = "";
    SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);

    cout << textBuffer;

最佳答案

另一种适用于 IE 实例的工作方法:

#include <stdio.h>
#include <wchar.h>
#include <Windows.h>
#include <Exdisp.h>

//This imports the shell extionsions
//we disable warnings of multiple defines
#pragma warning(disable: 4192)
#pragma warning(disable: 4146)
#import <mshtml.tlb>
#import <shdocvw.dll>

void PrintBrowserInfo(IWebBrowser2 *pBrowser) {
    //These functions return Unicode strings.
    BSTR bstr;
    //Get the window title
    pBrowser->get_LocationName(&bstr);
    wprintf(L"Title: %s\n", bstr);
    SysFreeString(bstr);
    //Detect if this is Windows Explorer (My Computer) or Internet Explorer (the internet)
    IDispatchPtr spDisp;
    char *type = "Windows Explorer";
    if (pBrowser->get_Document(&spDisp) == S_OK && spDisp != NULL) {
        MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
        if (spHtmlDocument != NULL) {
            MSHTML::IHTMLElementPtr spHtmlElement;
            spHtmlDocument->get_body(&spHtmlElement);
            if (spHtmlElement != NULL) {
                type = "Internet Explorer";
            }
        }
        spDisp.Release();
    }
    printf(" Type: %s\n", type);
    //Get the URL of the folder or web page
    pBrowser->get_LocationURL(&bstr);
    wprintf(L"  URL: %s\n\n", bstr);
    SysFreeString(bstr);
}

int main() {
    CoInitialize(NULL); //We need COM
    SHDocVw::IShellWindowsPtr spSHWinds;
    IDispatchPtr spDisp;
    //Find all explorer (Windows and Internet) and list them
    if (spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK) {
        long nCount = spSHWinds->GetCount();
        for (long i = 0; i < nCount; i++) {
            _variant_t va(i, VT_I4);
            spDisp = spSHWinds->Item(va);
            SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
            if (spBrowser != NULL) {
                //spBrowser->AddRef();
                PrintBrowserInfo((IWebBrowser2 *)spBrowser.GetInterfacePtr());
                spBrowser.Release();
            }
        }
    } else {
        puts("Shell windows failed to initialise");
    }
    system("PAUSE");
    return 0;
}

关于c++ - 如何在 C++ 中获取浏览器地址栏的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19281893/

相关文章:

c++ - QT 与不同的工具集

c++ - 头文件中的多重定义

javascript - Node.js fs.open() 在尝试打开超过 4 个命名管道 (FIFO) 后挂起

c++ - 为什么这个 C++ 片段编译(非 void 函数不返回值)

c++ - 您如何在托管代码环境之外安全地进行编程?

C++ 枚举与 unsigned int 比较

visual-studio-2012 - 有谁知道从哪里获得 [Stack Overflow Search] Visual Studio 扩展?

c++ - Visual Studio 2012 性能分析无法加载符号文件

resharper - Ctrl + T显示类(class)

c# - Visual Studio 2012 中缺少 SHA1(和整个密码学命名空间)