c# Reboot_CCBOOT監控工具
1.Form1 SOURCE CODE:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;//啟動模擬滑鼠鍵盤操作
using System.Data.SqlClient;
namespace Reboot_CCBOOT_V1._00
{
public delegate bool CallBack(int hwnd, int lParam);
public partial class Form1 : Form
{
public List<String> Cfg = new List<String>();
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
[DllImport("user32.dll")]
private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
//ShowWindow引數
private const int SW_SHOWNORMAL = 1;
private const int SW_RESTORE = 9;
private const int SW_SHOWNOACTIVATE = 4;
//SendMessage引數
private const int WM_KEYDOWN = 0X100;
private const int WM_KEYUP = 0X101;
private const int WM_SYSCHAR = 0X106;
private const int WM_SYSKEYUP = 0X105;
private const int WM_SYSKEYDOWN = 0X104;
private const int WM_CHAR = 0X102;
private const int WS_VISIBLE = 268435456;//窗體可見
private const int WS_MINIMIZEBOX = 131072;//有最小化按鈕
private const int WS_MAXIMIZEBOX = 65536;//有最大化按鈕
private const int WS_BORDER = 8388608;//窗體有邊框
private const int GWL_STYLE = (-16);//窗體樣式
private const int GW_HWNDFIRST = 0;
private const int GW_HWNDNEXT = 2;
private const int SW_HIDE = 0;
private const int SW_SHOW = 5;
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
private extern static int GetWindow(int hWnd, int wCmd);
[DllImport("User32.dll")]
private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect);
public Form1()
{
InitializeComponent();
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Location = new System.Drawing.Point(100, 100);
//獲取當前工作寬度和高度(工作區不包含狀態列)
int ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width;
int ScreenHeight = Screen.PrimaryScreen.WorkingArea.Height;
//計算窗體顯示的座標值,可以根據需要微調幾個畫素
int x = ScreenWidth - this.Width - 5;
int y = ScreenHeight - this.Height - 5;
this.Location = new Point(x, y);
}
private void Form1_Load(object sender, EventArgs e)
{
String strFullPath = Application.ExecutablePath;//讀取執行路徑
String strFileName = System.IO.Path.GetFileName(strFullPath);//讀取程式名稱
String FileName = strFileName.Substring(0,strFileName.Length-4);
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName(FileName);
if (myProcesses.Length > 1)//判斷程式是否啟動
{
MessageBox.Show("程式已啟動", "系統提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(1);
}
else
{
if (ReadCfg("Reboot_CCBOOT.config") == true)
{
timer1.Enabled = true;
}
else
{
MessageBox.Show("讀取配置錯誤", "系統提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(1);
}
}
}
public Boolean ReadCfg(String CfgFileName)//讀取配置
{
Boolean Flag = false;
try
{
FileStream fs = new FileStream(CfgFileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs,Encoding.Default);
String Temp = String.Empty;
while ((Temp = sr.ReadLine()) != null)
{
if (Temp[0] != '#')
{
String[] ArrayStr = Temp.Split(new String[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
if (ArrayStr.Length > 1)
{
Cfg.Add(ArrayStr[1].Trim());
}
}
}
sr.Close();
fs.Close();
Flag = true;
}
catch (Exception ex)
{
WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss "+ex.ToString()));
MessageBox.Show("讀取配置錯誤", "系統提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(1);
}
return Flag;
}
public void BoolMouse(int x, int y)//執行模擬滑鼠操作
{
SetCursorPos(x, y);
SendMouseEvent.Click();
}
public void WriteLog(String ErrText)//寫錯誤日誌
{
FileStream fs = new FileStream("Err.log",FileMode.Append,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(ErrText);
sw.Close();
fs.Close();
}
//判斷程序是否啟動
public Boolean RedProcess(String RunBathName)
{
Boolean Flag = false;
Process[] pro = Process.GetProcesses();//獲取已開啟的所有程序
for (int i = 0; i < pro.Length; i++)
{
if (pro[i].ProcessName.ToString().ToString() == RunBathName)
{
Flag = true;
break;
}
}
return Flag;
}
//呼叫執行檔案
public Boolean CallBath(String CallBathName)
{
Process proc = null;
string targetDir = string.Empty;
targetDir = System.IO.Directory.GetCurrentDirectory() + @"\";
Boolean Flag = false;
try
{
proc = new Process();
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = CallBathName;
proc.Start();
proc.WaitForExit();
Flag = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(1);
Flag = false;
}
return Flag;
}
//關閉程序式
public void KillProcess(string RunBathName)
{
Process[] pro = Process.GetProcesses(); //獲取已開啟的所有程序
for (int i = 0; i < pro.Length; i++)
{
//判斷此程序是否是要檢視的程序
if (pro[i].ProcessName.ToString().ToString() == RunBathName)
{
pro[i].Kill();//結束程序
}
}
}
//public String TitleName = String.Empty;
public Boolean GetRunWindowsText()//根據窗體控制代碼獲取窗體名稱
{
//StringBuilder s = new StringBuilder(512);
//if (GetWindowText(02098286, s, s.Capacity) == false)
// return false;
//TitleName = s.ToString();
IntPtr hwnd = FindWindow(null,Cfg[2]);
if (hwnd.ToString() == "0") return false;
return true;
}
public void CpyCCbootini()//制復CCboot.ini檔案
{
string targetDir = string.Empty;
targetDir = System.IO.Directory.GetCurrentDirectory() + @"\CCBoot.ini";
File.Copy(targetDir,@"C:\CCBoot\CCBoot.ini",true);
}
public uint Boot_i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (GetRunWindowsText() == false)
{
timer1.Enabled = false;
KillProcess(Cfg[3]);
CpyCCbootini();
CallBath(Cfg[0]);
timer2.Enabled = true;
}
else
{
label2.Text = "程式查詢" + Boot_i.ToString() + "數";
Boot_i++;
}
}
public uint Intercept = 0;
private void timer2_Tick(object sender, EventArgs e)
{
Intercept++;
label1.Text = "攔截重啟動"+Intercept.ToString()+"次CCBOOT";
this.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss " + "攔截重啟動" + Intercept.ToString() + "次CCBOOT"));
timer2.Enabled = false;
Boot_i = 0;
timer1.Enabled = true;
}
private void Form1_Shown(object sender, EventArgs e)//點選顯示窗體
{
this.BeginInvoke(new Action(() =>
{
this.Hide();
this.Opacity = 1;
}));
}
}
}
2.SendMouseEvent SOURCED CODE:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Reboot_CCBOOT_V1._00
{
public class SendMouseEvent : Form1
{
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
[Flags]
public enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
public static void Send(MouseEventFlag mouseEventFlag, int dx, int dy, uint dwData)
{
mouse_event(mouseEventFlag | MouseEventFlag.Absolute, dx, dy, dwData, UIntPtr.Zero);
}
public static void MoveTo(uint scceenTop, uint screenLeft)
{
int x = scceenTop == 0 ? 0 : (int)((float)scceenTop / (float)Screen.PrimaryScreen.Bounds.Height * (float)65535);
int y = screenLeft == 0 ? 0 : (int)((float)screenLeft / (float)Screen.PrimaryScreen.Bounds.Width * (float)65535);
mouse_event(MouseEventFlag.Move | MouseEventFlag.Absolute, x, y, 0, UIntPtr.Zero);
}
public static void Click()
{
LeftDown(); LeftUp();
}
public static void DoubleClick()
{
Click(); Click();
}
public static void LeftDown()
{
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
}
public static void LeftUp()
{
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
public static void RightDown()
{
mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
}
public static void RightUp()
{
mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
}
public static void MiddleDown()
{
mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
}
public static void MiddleUp()
{
mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
}
}
public class FromInfo : Form1
{
private String title;
private int handle;
public FromInfo(String title, int handle)
{
this.title = title;
this.handle = handle;
}
public String Title
{
get { return title; }
set { title = value; }
}
public int Handle
{
get { return handle; }
set { handle = value; }
}
}
}
3.CONFIG:
4.程式執行窗面: