-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
145 lines (121 loc) · 4.33 KB
/
Program.cs
File metadata and controls
145 lines (121 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
static class Program
{
[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_HIDE = 0;
private const int SW_SHOW = 5;
[STAThread]
static void Main()
{
// 先隐藏控制台窗口
var handle = GetConsoleWindow();
ShowWindow(handle, SW_SHOW);
// 判断管理员权限
if (!IsAdministrator())
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = Assembly.GetExecutingAssembly().Location,
UseShellExecute = true,
Verb = "runas"
};
Process.Start(startInfo);
}
catch
{
MessageBox.Show("需要管理员权限才能运行此程序。", "权限不足", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var config = YamlConfigLoader.LoadConfig();
var timeout = config.timeout;
var startup = config.startup;
var settings = config.settings;
var wait = config.wait;
var debug = config.debug;
var exit = config.exit;
// 注册退出时要执行的逻辑(你可以写自己的释放逻辑)
TrayIconApp.RunTrayIconInBackground(() =>
{
// 这是退出前你希望执行的代码
try
{
DriverLoader.InitializeDriver();
EcAccess.Init();
foreach (var dict in config.exit)
{
foreach (var kv in dict)
{
// key 按10进制转换
byte keyByte = Convert.ToByte(kv.Key, 10);
// value 按16进制转换,直接转换,不用去除0x
byte valueByte = Convert.ToByte(kv.Value, 16);
EcAccess.WriteEC(keyByte, valueByte, wait);
}
}
// 向 EC 寄存器 0x93 写入 0x55
}
catch (Exception ex)
{
MessageBox.Show("EC 写入失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// 比如关闭定时器、保存配置、断开驱动连接等
});
// 如果debug为true,显示控制台窗口
if (!debug)
{
ShowWindow(handle, SW_HIDE);
}
AutoStartHelper.SetAutoStart(startup);
while (true)
{
// 调用Ring0Init,确保驱动加载
try
{
DriverLoader.InitializeDriver();
EcAccess.Init();
foreach (var dict in config.settings)
{
foreach (var kv in dict)
{
// key 按10进制转换
byte keyByte = Convert.ToByte(kv.Key, 10);
// value 按16进制转换,直接转换,不用去除0x
byte valueByte = Convert.ToByte(kv.Value, 16);
EcAccess.WriteEC(keyByte, valueByte, wait);
}
}
// 向 EC 寄存器 0x93 写入 0x55
}
catch (Exception ex)
{
MessageBox.Show("EC 写入失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// === 你要执行的逻辑 ===
Console.WriteLine("跑一次:" + DateTime.Now);
Thread.Sleep(timeout);
}
}
static bool IsAdministrator()
{
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}