如何在 c++ 中获取浏览器地址栏的内容
How can I grab the contents of a browsers address bar in c++?
如果是 C++ 中的 Web 浏览器,我需要抓取活动 Windows 地址栏的内容。 我已经想出了如何抓取标题,并且可以抓取记事本的内容,但我被困在浏览器上。
我的目标是为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: %sn", 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: %sn", type);
//Get the URL of the folder or web page
pBrowser->get_LocationURL(&bstr);
wprintf(L" URL: %snn", 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;
}
所以看起来我为IE工作,仍然在FireFox和Chrome上工作。
这是IE的代码
HWND foreground = GetForegroundWindow();
HWND hwndEdit = FindWindowEx(foreground, NULL, "EDIT", NULL);
HWND handle = FindWindowEx(foreground, NULL, "WorkerW", "Navigation Bar");
if (NULL != handle)
{
handle = FindWindowEx(handle, NULL, "ReBarWindow32", NULL);
if (NULL != handle)
{
handle = FindWindowEx(handle, NULL, "Address Band Root", NULL);
}}
HWND hwndEdit = FindWindowEx(handle, NULL, "Edit", NULL);
const int bufferSize = 5024;
Char textBuffer[bufferSize] = "";
SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
string addressbartext = textBuffer;
if(addressbartext == "AutoCompleteProxy")
{addressbartext = "";}
else
{addressbartext = textBuffer;
}
cout << addressbartext;
请参阅:检查访问了哪个网站,但如果找到更好的解决方案,请告诉我,尤其是对于 Chrome。
相关文章:
- 将数组的地址分配给变量并删除
- 空基优化子对象的地址
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 如何在c++程序中找到函数的地址
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 被解释为低级别const的const对象的地址
- 将地址分配给本地指针后,公共对象的变量将消失
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 内联程序集printf将整数解释为地址
- 为什么指针不写入类的地址?
- 如何在C++中获取该对象的类声明中对象的地址?
- 通过按地址访问变量
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 函数名是c中该函数的第一条指令的地址吗
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 访问谷歌浏览器网址栏
- 如何在 c++ 中获取浏览器地址栏的内容
- 检索 Windows 资源管理器的地址栏编辑控件的句柄
- 发送文本到谷歌chrome地址栏