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程序弹窗错误,并重启的更多相关文章

  1. windows下调用外部exe程序 SHELLEXECUTEINFO

    本文主要介绍两种在windows下调用外部exe程序的方法: 1.使用SHELLEXECUTEINFO 和 ShellExecuteEx SHELLEXECUTEINFO 结构体的定义如下: type ...

  2. C#和asp.net执行外部EXE程序

    这两天研究下.Net的执行外部EXE程序问题,就是在一个程序里通过按钮或其他操作运行起来另外一个程序,需要传入参数,如用户名.密码之类(实际上很类似单点登录,不过要简单的多的多):总结如下: 1.CS ...

  3. c# 调用外部exe程序

    c#调用外部exe程序,首先要 using System.Diagnostics; 然后开启一个新process System.Diagnostics.ProcessStartInfo p=null; ...

  4. C# 将外部exe程序 嵌入到自己的窗体界面

    将别人开发的exe程序,放到自己的窗体里面来运行. 1.基本功能实现 首先,在自己的窗体后面加上代码: [DllImport("User32.dll", EntryPoint = ...

  5. WPF 程序中启动和关闭外部.exe程序

    当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来: C#后台代码如下: using System; using System.Collections.Generic; ...

  6. [转]VC中调用外部exe程序方式

    本文转自:http://blog.sina.com.cn/s/blog_486285690100ljwu.html 目前知道三种方式:WinExec,ShellExecute ,CreateProce ...

  7. 启动外部exe程序

    Process myProcess = new Process();myProcess.StartInfo.FileName = pathName;myProcess.Start();其中的pathN ...

  8. photoshop cs6安装过程中安装程序遇到错误:请重启计算机,解决办法

    1.关闭防火墙和杀毒软件 2.删除注册表 依次展开HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager目录,找到其中的 ...

  9. sqlserver数据库触发器调用外部exe

    sqlserver数据库触发器调用外部exe,同事可以选择参入参数! sqlserver使用 master..xp_cmdshell 进行外部exe的执行. 使用master..xp_cmdshell ...

随机推荐

  1. ymal文档格式 处理

    Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块. 参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation

  2. svn服务器用户名密码更改后,如何更新本地用户名密码

    在提交时,IDE会给出这样的提示,说明用户名密码已更改 在命令行输入 svn ls https:XXX(项目的地址),具体步骤如下图

  3. SpringBoot项目使用Nacos作为配置中心

    前置条件:jdk.SpringBoot项目.Nacos.Linux服务器(可无) 具体版本:jdk11.SpringBoot 2.3.5.RELEASE.Nacos 2.0.3.Centos 6 目标 ...

  4. 使用docker创建含有FFmpeg的自定义镜像

    Dockerfile文件 FROM openjdk:8-jre-alpine MAINTAINER "yvioo" RUN echo "http://mirrors.al ...

  5. c++内存分布之虚析构函数

    关于 本文代码演示环境: VS2017+32程序 虚析构函数是一种特殊的虚函数,可以知道,虚函数影响的内存分布规律应该也适用虚析构函数.看看实际结果. Note,一个类中,虚析构函数只能有一个. 本文 ...

  6. 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...

  7. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  8. 【LeetCode】677. Map Sum Pairs 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 前缀树 日期 题目地址:https://lee ...

  9. B. Arpa's weak amphitheater and Mehrdad's valuable Hoses

    B. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...

  10. 更快的Maven来了,我的天,速度提升了8倍!

    周末被 maven-mvnd 刷屏了,于是我也下载了一个 mvnd 体验了一把.虽然测试的数据都是基于我本地项目,不具备普适性和权威性,但也足以说明问题.它的测试结果远远超出我的预期,下面一起来看. ...