Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
101 lines (91 loc) · 3.29 KB
/
Copy pathProgram.cs
File metadata and controls
101 lines (91 loc) · 3.29 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
using System.Diagnostics;
using System.Security.Principal;
using System.Runtime.InteropServices;
namespace CpuTuner;
internal static class Program
{
[DllImport("user32.dll")]
private static extern bool SetProcessDpiAwarenessContext(IntPtr dpiAwarenessContext);
[STAThread]
private static int Main(string[] args)
{
if (!OperatingSystem.IsWindows())
return 1;
// Per-Monitor V2 DPI awareness so taskbar coordinates are exact.
try { SetProcessDpiAwarenessContext(new IntPtr(-4)); } catch { }
if (args.Length > 0 && args[0] == "--list")
{
try { Console.OutputEncoding = System.Text.Encoding.UTF8; } catch { }
HardwareReader.DumpSensors();
return 0;
}
// Second instance -> exit (avoids two widgets / two driver clients stacking).
using (var mutex = new Mutex(true, "CpuTuner_SingleInstance", out bool createdNew))
{
if (!createdNew)
return 0;
if (!IsAdministrator() && Environment.GetEnvironmentVariable("CPUTUNER_NOELEVATE") != "1")
{
try
{
var info = new ProcessStartInfo
{
FileName = Environment.ProcessPath,
UseShellExecute = true,
Verb = "runas",
};
foreach (string a in args)
info.Arguments = string.IsNullOrEmpty(info.Arguments) ? a : info.Arguments + " " + a;
Process.Start(info);
}
catch (Exception)
{
MessageBox.Show(
"CpuTuner 需要管理员权限读取传感器并修改 CPU 功耗限制。\r\n\r\n" +
"请右键“以管理员身份运行”,或在弹出的 UAC 提示中点击“是”。",
"CpuTuner",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
return 0;
}
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
ApplicationConfiguration.Initialize();
AppDomain.CurrentDomain.UnhandledException += (_, e) => ShowFatal(e.ExceptionObject as Exception);
Application.ThreadException += (_, e) => ShowFatal(e.Exception);
AppCore? app = null;
try
{
app = new AppCore();
}
catch (Exception ex)
{
ShowFatal(ex);
return 1;
}
Application.Run(app);
}
return 0;
}
private static bool IsAdministrator()
{
using var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
private static void ShowFatal(Exception? ex)
{
try
{
MessageBox.Show(
ex?.ToString() ?? "未知错误",
"CpuTuner 错误",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
catch
{
// nothing else we can do
}
}
}