C#检测外部exe程序弹窗错误,并重启
private void button2_Click(object sender, EventArgs e)
{ string mainTitle = System.Configuration.ConfigurationManager.AppSettings["mainTitle"];
//弹框的class名称可以用Spy++程序来获取
string mainClassName = System.Configuration.ConfigurationManager.AppSettings["mainClassName"];
string labelClassName = System.Configuration.ConfigurationManager.AppSettings["labelClassName"];
string buttonTxt = System.Configuration.ConfigurationManager.AppSettings["buttonTxt"];
//MessageBox弹框
var isFind = FindException(mainTitle, mainClassName, labelClassName, buttonTxt); if (isFind == false)
{
var list = System.Diagnostics.Process.GetProcessesByName("Win.Process");
var sb1 = new StringBuilder(100);
foreach (var item in list)
{
GetClassNameW(item.MainWindowHandle, sb1, sb1.Capacity);
sb1.Append(",");
}
string dynamicName = sb1.ToString().Split('.').Last().Replace(",", "");
mainClassName = "WindowsForms10.Window.8.app.0." + dynamicName;
labelClassName = "WindowsForms10.STATIC.app.0." + dynamicName;
buttonTxt = "退出(&Q)";
//未捕获异常弹窗
FindException(mainTitle, mainClassName, labelClassName, buttonTxt);
} }
/// <summary>
/// 获取报错弹窗,记录日志,然后重启目标程序
/// </summary>
/// <param name="mainTitle"></param>
/// <param name="mainClassName"></param>
/// <param name="labelClassName"></param>
/// <param name="buttonTxt"></param>
/// <returns></returns>
private bool FindException(string mainTitle, string mainClassName, string labelClassName, string buttonTxt)
{ appWin = FindWindow(mainClassName, mainTitle);
if (appWin != IntPtr.Zero)
{
IntPtr childHwnd = FindWindowEx(appWin, IntPtr.Zero, null, buttonTxt); if (childHwnd != IntPtr.Zero)
{
var sb = new StringBuilder(500); var label = FindWindowEx(appWin, IntPtr.Zero, labelClassName, null);
if (label != IntPtr.Zero)
{
GetWindowText(label, sb, sb.Capacity); var log = $"\n{DateTime.Now}\n获取的异常:{ sb.ToString()}\n";
richTextBox1.AppendText(log);
richTextBox1.ScrollToCaret(); System.IO.File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory +"/"+ DateTime.Now.ToString("yyyyMMdd")+ ".txt", log);
//SendEmail( $"【{mainTitle}】出现异常:{sb.ToString()}"); }
SendMessage(childHwnd, BM_CLICK, 0, 0);
SendMessage(childHwnd, BM_CLICK, 0, 0);
System.Threading.Thread.Sleep(1000);
//重启程序
System.Diagnostics.Process.Start(_targetExePath); }
return true;
}
return false;
}
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
//窗口发送给按钮控件的消息,让按钮执行点击操作,可以模拟按钮点击
private const int BM_CLICK = 0xF5;
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
//获取窗口类名
[DllImport("user32.dll")]
private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings> <add key="mainTitle" value="xxx系统"/>
<add key="mainClassName" value="#32770"/>
<add key="labelClassName" value="Static"/>
<add key="buttonTxt" value="确定"/>
<add key="targetExePath" value="D:\xxx.exe"/>
</appSettings>
</configuration>
/*
文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放 文字少不能投放
*/
C#检测外部exe程序弹窗错误,并重启的更多相关文章
- windows下调用外部exe程序 SHELLEXECUTEINFO
本文主要介绍两种在windows下调用外部exe程序的方法: 1.使用SHELLEXECUTEINFO 和 ShellExecuteEx SHELLEXECUTEINFO 结构体的定义如下: type ...
- C#和asp.net执行外部EXE程序
这两天研究下.Net的执行外部EXE程序问题,就是在一个程序里通过按钮或其他操作运行起来另外一个程序,需要传入参数,如用户名.密码之类(实际上很类似单点登录,不过要简单的多的多):总结如下: 1.CS ...
- c# 调用外部exe程序
c#调用外部exe程序,首先要 using System.Diagnostics; 然后开启一个新process System.Diagnostics.ProcessStartInfo p=null; ...
- C# 将外部exe程序 嵌入到自己的窗体界面
将别人开发的exe程序,放到自己的窗体里面来运行. 1.基本功能实现 首先,在自己的窗体后面加上代码: [DllImport("User32.dll", EntryPoint = ...
- WPF 程序中启动和关闭外部.exe程序
当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来: C#后台代码如下: using System; using System.Collections.Generic; ...
- [转]VC中调用外部exe程序方式
本文转自:http://blog.sina.com.cn/s/blog_486285690100ljwu.html 目前知道三种方式:WinExec,ShellExecute ,CreateProce ...
- 启动外部exe程序
Process myProcess = new Process();myProcess.StartInfo.FileName = pathName;myProcess.Start();其中的pathN ...
- photoshop cs6安装过程中安装程序遇到错误:请重启计算机,解决办法
1.关闭防火墙和杀毒软件 2.删除注册表 依次展开HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager目录,找到其中的 ...
- sqlserver数据库触发器调用外部exe
sqlserver数据库触发器调用外部exe,同事可以选择参入参数! sqlserver使用 master..xp_cmdshell 进行外部exe的执行. 使用master..xp_cmdshell ...
随机推荐
- Django 中间件理解
中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 应用场景,对所有 ...
- tcping和tcpping工具使用
tcping和tcpping工具 1.工具使用 1.1.windows版tcping 1.2.linux版tcpping 2.B站问题(linux版本tcpping探测ip,且ip无法解析到主机名) ...
- 深入理解css中position属性及z-index属性 https://www.cnblogs.com/zhuzhenwei918/p/6112034.html
深入理解css中position属性及z-index属性 请看出处:https://www.cnblogs.com/zhuzhenwei918/p/6112034.html 在网页设计中,positi ...
- 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
- 【LeetCode】212. Word Search II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
- Dubbo 的设计思想
在java远程调用多年的沉淀 <1>首先是socket调用.在orderService中开放socket服务,在userService中进行远程调用. 优点:解决了单机调用的问题. 缺点: ...
- WEB文档在线预览解决方案
web页面无法支持预览office文档,但是却可以预览PDF.flash文档,所以大多数解决方案都是在服务端将office文档转换为pdf,然后再通过js的pdf预览插件(谷歌浏览器等已经原生支持嵌入 ...
- [opencv]drawContours 示例
vector<vector<Point>> contours; vector<Vec4i> hierarchy; findContours(img_canny,co ...
- Java的generator工具类,数据库生成实体类和映射文件
首先需要几个jar包: freemarker-2.3.23.jar log4j-1.2.16.jar mybatis-3.2.3.jar mybatis-generator-core-1.3.2.ja ...