-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerManager.cs
More file actions
105 lines (90 loc) · 3.65 KB
/
Copy pathPowerManager.cs
File metadata and controls
105 lines (90 loc) · 3.65 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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace CPUBoostManager
{
public class PowerManager
{
[DllImport("powrprof.dll")]
private static extern uint PowerSetActiveScheme(IntPtr UserRootPowerKey, ref Guid SchemeGuid);
[DllImport("powrprof.dll")]
private static extern uint PowerGetActiveScheme(IntPtr UserRootPowerKey, out IntPtr ActivePolicyGuid);
[DllImport("powrprof.dll")]
private static extern uint PowerWriteACValueIndex(IntPtr RootPowerKey, ref Guid SchemeGuid,
ref Guid SubGroupOfPowerSettingsGuid, ref Guid PowerSettingGuid, uint AcValueIndex);
// Processor power management
private static readonly Guid GUID_PROCESSOR_SUBGROUP =
new("54533251-82be-4824-96c1-47b60b740d00");
// Processor performance boost mode
private static readonly Guid GUID_PROCESSOR_BOOST_POLICY =
new("be337238-0d82-4146-a960-4f3749d470c7");
// Boost values
private const uint BOOST_DISABLED = 0;
private const uint BOOST_EFFICIENT_AGGRESSIVE = 1; // Efficient Aggressive
private const uint BOOST_ENABLED = 2; // Aggressive
private Guid _currentScheme;
private bool _boostEnabled = true;
public PowerManager()
{
// Get current power scheme
IntPtr ptr = IntPtr.Zero;
var result = PowerGetActiveScheme(IntPtr.Zero, out ptr);
if (result == 0 && ptr != IntPtr.Zero)
{
_currentScheme = Marshal.PtrToStructure<Guid>(ptr);
Marshal.FreeHGlobal(ptr);
Console.WriteLine("Power Manager initialized successfully.");
}
else
{
throw new Exception("Failed to get active power scheme.");
}
}
public void DisableBoost()
{
if (!_boostEnabled) return;
Console.WriteLine("Disabling CPU Boost...");
var subgroup = GUID_PROCESSOR_SUBGROUP;
var boost_policy = GUID_PROCESSOR_BOOST_POLICY;
var result = PowerWriteACValueIndex(
IntPtr.Zero,
ref _currentScheme,
ref subgroup,
ref boost_policy,
BOOST_DISABLED);
if (result == 0)
{
PowerSetActiveScheme(IntPtr.Zero, ref _currentScheme);
_boostEnabled = false;
Console.WriteLine("CPU Boost disabled successfully.");
}
else
{
Console.WriteLine($"Failed to disable CPU Boost. Error code: {result}");
}
}
public void EnableBoost()
{
if (_boostEnabled) return;
Console.WriteLine("Enabling CPU Boost...");
var subgroup = GUID_PROCESSOR_SUBGROUP;
var boost_policy = GUID_PROCESSOR_BOOST_POLICY;
var result = PowerWriteACValueIndex(
IntPtr.Zero,
ref _currentScheme,
ref subgroup,
ref boost_policy,
BOOST_EFFICIENT_AGGRESSIVE);
if (result == 0)
{
PowerSetActiveScheme(IntPtr.Zero, ref _currentScheme);
_boostEnabled = true;
Console.WriteLine("CPU Boost enabled successfully.");
}
else
{
Console.WriteLine($"Failed to enable CPU Boost. Error code: {result}");
}
}
}
}