通过钩子程序跨程序关闭Window
需求:
在实际场景中会有自身程序在调用第三方的动态库过程中,因为第三方的动态库弹框导致线程阻塞,必须手动将弹窗关闭后才能回到自身程序的主线程中。
最简单的场景就是很多自助设备,本身是没有固定操作员的,如果用户在看到弹框后没有手动点击关闭则弹框会一直会存在。
解决方案:
1、通过Windows提供的API,FindWindow(通过Window的Title)获取到第三方弹框句柄,通过SendMessage向Winwos发起关闭该句柄的命令;
2、如果该弹框有“关闭“按钮,则通过FindWindow获取到句柄后,再通过FindwWindowEx找到“关闭”按钮子句柄,然后通过SendMessage发起鼠标左击事件;
Code:
方案1:
/// <summary>
/// 寻找系统的全部窗口
/// </summary>
/// <returns></returns>
public WindowInfo[] GetAllDesktopWindows()
{
List<WindowInfo> wndList = new List<WindowInfo>();
EnumWindows(delegate (IntPtr hWnd, int lParam)
{
WindowInfo wnd = new WindowInfo();
StringBuilder sb = new StringBuilder(256);
//get hwnd
wnd.hWnd = hWnd;
//get window name
GetWindowTextW(hWnd, sb, sb.Capacity);
wnd.szWindowName = sb.ToString();
//get window class
GetClassNameW(hWnd, sb, sb.Capacity);
wnd.szClassName = sb.ToString();
//add it into list
wndList.Add(wnd);
return true;
}, 0);
return wndList.ToArray();
} /// <summary>
/// 根据指定窗口标题暴力干掉这个窗口
/// </summary>
/// <param name="windowTitle">window的标题</param>
/// <param name="second">指定多少秒后关闭window</param>
/// <returns></returns>
public async Task KillWindow(string windowTitle, int second = 3)
{
try
{
await Task.Delay(TimeSpan.FromSeconds(second));
#region 通过获取所有窗口方式关闭 WindowInfo[] a = GetAllDesktopWindows();
int i = 0;
int index = 0;
for (i = 0; i < a.Length; i++)
{
if (a[i].szWindowName.ToString().Contains(windowTitle))
{
index = i;
IntPtr myIntPtr = a[index].hWnd;
//先置顶!!!不置顶发送windows消息没卵用
SetWindowPos(myIntPtr, -1, 0, 0, 0, 0, 1 | 2);
if (myIntPtr != null)
{
IntPtr childHwnd = FindWindowEx(myIntPtr, IntPtr.Zero, null, "否(&N)");
SendMessage(childHwnd, BM_CLICK, 0, 0); //发送点击按钮的消息
}
break;
}
}
#endregion
}
catch (Exception)
{
}
}
方案二:
/// <summary>
/// 根据指定窗口标题暴力干掉这个窗口
/// </summary>
/// <param name="windowTitle">window的标题</param>
/// <param name="second">指定多少秒后关闭window</param>
/// <returns></returns>
public async Task KillWindowAndBtn(string windowTitle, int second = 3, string btnTxt = "否(&N)")
{
try
{
await Task.Delay(TimeSpan.FromSeconds(second));
#region 通过获取所有窗口方式关闭 WindowInfo[] a = GetAllDesktopWindows();
int i = 0;
int index = 0;
for (i = 0; i < a.Length; i++)
{
if (a[i].szWindowName.ToString().Contains(windowTitle))
{
index = i;
IntPtr myIntPtr = a[index].hWnd;
//先置顶!!!不置顶发送windows消息没卵用
SetWindowPos(myIntPtr, -1, 0, 0, 0, 0, 1 | 2);
if (myIntPtr != null)
{
IntPtr childHwnd = FindWindowEx(myIntPtr, IntPtr.Zero, null, btnTxt);
SendMessage(childHwnd, BM_CLICK, 0, 0); //发送点击按钮的消息
}
break;
}
}
#endregion
}
catch (Exception)
{
}
}
调用方式(第一种方案):
private async void KillCardErrorWindow()
{
WinUtils utils = new WinUtils();
//通过获取全部窗口方式进行模糊查询
await utils.KillWindow("卡");
}
调用第二种方案:
/// <summary>
/// 杀掉医保读卡dll生成的window
/// </summary>
private async void KillPassWordWindow()
{
WinUtils utils = new WinUtils();
//通过获取全部窗口方式进行模糊查询
await utils.KillWindow("输入密码"); //"读市民卡错误提示:"
}
PS: 无论使用哪一种方案,都必须在调用第三方API之前先发起杀掉window的方法,留意代码即可知道,都是通过异步执行的。
所以结合实际场景需要,请先设定Task.Delay的时间值。
通过钩子程序跨程序关闭Window的更多相关文章
- MemoryMappedFile 在IIS与程序跨程序交互数据的权限问题
使用IIS 与程序交互时,发布到IIS上获取不到数据提供方的数据(VSF5运行可以获取到数据),MemoryMappefFile基本使用不做介绍 数据方 static void Main(string ...
- Android入门(十四)内容提供器-实现跨程序共享实例
原文链接:http://www.orlion.ga/661/ 打开SQLite博文中创建的 DatabaseDemo项目,首先将 MyDatabaseHelper中使用 Toast弹出创建数据库成功的 ...
- android: 实现跨程序数据共享
简单起见,我们还是在上一章中 DatabaseTest 项目的基础上继续开发,通过内容提供器 来给它加入外部访问接口. 打开 DatabaseTest 项目,首先将 MyDatabaseHelper ...
- CreateWindowEx failed (当前程序已使用了 Window 管理器对象的系统允许的所有句柄。)
我在QT图形场景视图中通过QGraphicsProxyWidget添加代理Widget(实现添加基本的QT Widget,如按钮.复选框.日期时间控件等),当数量超过3500左右的时候,QT应用程序直 ...
- Android学习--跨程序共享数据之内容提供其探究
什么是内容提供器? 跨程序共享数据之内容提供器,这是个什么功能?看到这个名称的时候最能给我们提供信息的应该是“跨程序”这个词了,是的重点就是这个词,这个内容提供器的作用主要是用于在不同的引用程序之间实 ...
- C# 最基本的涉及模式(单例模式) C#种死锁:事务(进程 ID 112)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品。请重新运行该事务,解决方案: C#关闭应用程序时如何关闭子线程 C#中 ThreadStart和ParameterizedThreadStart区别
C# 最基本的涉及模式(单例模式) //密封,保证不能继承 public sealed class Xiaohouye { //私有的构造函数,保证外部不能实例化 private ...
- Java基础-输入输出-2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt
2.编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt ...
- JAVA基础-输入输出:1.编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。
1.编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上. package Test03; ...
- C#:手把手教你用C#打包应用程序(安装程序卸载程序)
摘要:本文介绍在C#中手把手教你用C#打包应用程序(安装程序卸载程序) 1:新建安装部署项目 打开VS,点击新建项目,选择:其他项目类型->安装与部署->安装向导(安装项目也一样),然后点 ...
随机推荐
- Layui下拉框改变时触发事件
layui.use(['form', 'layer'], function () { var form = layui.form(); var layer = layui.layer; form.on ...
- xlrd模块
import xlrdbook = xlrd.open_workbook('app_student.xls')sheet = book.sheet_by_index(0)# sheet2 = book ...
- Mesos源码分析(9): Test Framework的启动
我们以Test Framework为例子解释Framework的启动方式. Test Framework的代码在src/examples/test_framework.cpp中的main函数 首先要指 ...
- 实战经验丨PHP反序列化漏洞总结
又到了金三银四跳槽季,很多小伙伴都开始为面试做准备,今天小编就给大家分享一个网安常见的面试问题:PHP反序列化漏洞. 虽然PHP反序列化漏洞利用的条件比较苛刻,但是一旦被利用就会产生很严重的后果,所以 ...
- Python 爬虫入门(一)——爬取糗百
爬取糗百内容 GitHub 代码地址https://github.com/injetlee/Python/blob/master/qiubai_crawer.py 微信公众号:[智能制造专栏],欢迎关 ...
- [Swift]LeetCode126. 单词接龙 II | Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- [Swift]LeetCode218. 天际线问题 | The Skyline Problem
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [Swift]LeetCode928. 尽量减少恶意软件的传播 II | Minimize Malware Spread II
(This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of ...
- [Swift]LeetCode975. 奇偶跳 | Odd Even Jump
You are given an integer array A. From some starting index, you can make a series of jumps. The (1 ...
- 互联网最新kafka技术面试题含答案
1.Kafka 的设计时什么样的呢? Kafka 将消息以 topic 为单位进行归纳 将向 Kafka topic 发布消息的程序成为 producers. 将预订 topics 并消费消息的程序成 ...