C# 自写程序报错后重启 程序运行起来没问题 但就是报错

qq_32398257 2016-12-08 05:57:24
[DllImport("wininet.dll", EntryPoint = "InternetGetConnectedState")]
public extern static bool InternetGetConnectedState(out int conState, int reder);
[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);


static List<string> list = new List<string>();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);

[DllImport("user32.dll")]
public static extern int GetWindowText(int hWnd, IntPtr lpString, int nMaxCount);
public delegate bool CallBack(int hwnd, int lParam);
public delegate bool EnumWindowsProc(int hWnd, int lParam);
[DllImport("user32.dll")]
public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);
[DllImport("user32.dll")]
public static extern int EnumWindows(CallBack lpfn, int lParam);
[DllImport("user32.dll")]
public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);

static void Main(string[] args)
{
try {
IntPtr hwnd = FindWindow(null, "测试系统");
int cc = hwnd.GetHashCode();

string error="通讯链接失败";
string lujing = @"C:\Program Files\测试系统\fwtel.exe";
EnumChildWindows(hwnd, EnumWindowsMethod, IntPtr.Zero);
foreach(var l in list ){


if (l.IndexOf(error) != -1 && l.IndexOf("运行时错误")!=-1)
{
StreamWriter sw = File.AppendText("E:\\ErrorLog\\Log.txt");
string w = "通讯链接失败 错误时间:" + DateTime.Now+"\n";
sw.WriteLine(w);
sw.Close();

Process[] processes;
processes = Process.GetProcesses();
string str = "";
foreach (Process p in processes)
{
try
{
str = p.MainModule.FileName;
if (str.Equals(lujing,StringComparison.CurrentCultureIgnoreCase))
{
p.CloseMainWindow();
p.Kill();
StreamWriter swww = File.AppendText("E:\\ErrorLog\\Log.txt");
string www = "程序关闭 操作时间:" + DateTime.Now + "";
swww.WriteLine(www);
swww.Close();


Process.Start(lujing);
StreamWriter swwww = File.AppendText("E:\\ErrorLog\\Log.txt");
string wwww = "程序重新启动 操作时间:" + DateTime.Now + "";
swwww.WriteLine(wwww);
swwww.Close();
}
}
catch (Exception ex)
{
StreamWriter sww = File.AppendText("E:\\ErrorLog\\Log.txt");
string ww = "程序错误 错误问题"+ex.ToString()+"错误时间:" + DateTime.Now + "";
sww.WriteLine(ww);
sww.Close();
}
}

}


}

}
catch (Exception ex)
{
StreamWriter sww = File.AppendText("E:\\ErrorLog\\Log.txt");
string ww = "程序错误 错误问题" + ex.ToString() + "错误时间:" + DateTime.Now + "\n";
sww.WriteLine(ww);
sww.Close();
}

}



private static bool EnumWindowsMethod(int hWnd, int lParam)
{
IntPtr lpString = Marshal.AllocHGlobal(200);
GetWindowText(hWnd, lpString, 200);
var text = Marshal.PtrToStringAnsi(lpString);
if (!string.IsNullOrWhiteSpace(text))
list.Add(text);
return true;
}


这是日记里面的内容
是不是系统太低级了



通讯链接失败 错误时间:2016-12-8 9:00:00

程序关闭 操作时间:2016-12-8 9:00:00
程序重新启动 操作时间:2016-12-8 9:00:00
程序错误 错误问题System.ComponentModel.Win32Exception (0x80004005): Unable to enumerate the process modules.
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
at System.Diagnostics.Process.get_MainModule()
at Gprocess.Program.Main(String[] args)错误时间:2016-12-8 9:00:00
程序错误 错误问题System.ComponentModel.Win32Exception (0x80004005): Unable to enumerate the process modules.
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
at System.Diagnostics.Process.get_MainModule()
at Gprocess.Program.Main(String[] args)错误时间:2016-12-8 9:00:00

...全文
252 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
可能是你的动态库有和其他的dll有引用关系而你本机缺少相应的dll,你可以去我的资源里面下载“查看动态库引用关系”的工具检查一下
  • 打赏
  • 举报
回复
程序没有挂是因为你tyr catch捕获了异常。你代码里面有两个tyr catch,不知道是哪个报错。根据报错信息好像是processes = Process.GetProcesses();这句,无法获得模块信息,你debug看看是哪里异常。有可能是你的程序权限不够,后者被杀毒软件拦截。
微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 微信小程序系统教程共有“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[中级阶段]——核心技术”、“微信小程序系统教程[阶段]客服消息+微信支付+九宝电商系统”。 “微信小程序系统教程[阶段]全套课程”包含: 1.微信小程序系统教程[阶段]_客服消息 2.微信小程序系统教程[阶段]_微信支付 3.微信小程序系统教程[阶段]_九宝电商系统 学习“微信小程序系统教程[阶段]”要求有微信小程序的基础。建议先学习“微信小程序系统教程[初级阶段]”、“微信小程序系统教程[中级阶段]”,后在报名“微信小程序系统教程[阶段]”。 阶段讲解的客服消息,是针对小程序的。后台程序用接近底层的技术,有使用三方技术。这样降低同学们学习成本。 微信支付,这部分课程很有难度,要求同学们认真听讲,有不会的技术问题可以请教老师。购买课程后请联系老师,提供单号,给你源程序。 九宝电商系统是一套十分适和学习、项目的课程。既可以系统的学习微信小程序相关知识,还可以修改后上线。 “微信小程序系统教程[中级阶段]——核心技术”重点讲解微信小程序事件、组件、API 微信小程序系统教程[初级阶段],微信小程序0基础学起,讲解微信小程序开发的基础知识。 购买课程的同学,可赠送就九宝老师编写的《微信小程序开发宝典》。 购课请咨询qq2326321088

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧