internal static void Main_MessageReceived(object sender, EventArgsValue <Message> e) { if (e.Value.Msg != (int)Classes.System.WindowsMessages.WM_POWERBROADCAST) { return; } PBT wParam = (PBT)e.Value.WParam.ToInt32(); switch (wParam) { case PBT.POWERSETTINGCHANGE: PowerBroadcast_Setting pbs = (PowerBroadcast_Setting)Marshal.PtrToStructure(e.Value.LParam, typeof(PowerBroadcast_Setting)); PowerScheme oldPlan = (PowerScheme)oldStaticValues[EventType.PowerSchemeChanged]; PowerScheme newPlan = Status.Power.ActivePowerScheme; if (oldPlan == newPlan) { return; } if (OnPowerSchemeChanged != null) { OnPowerSchemeChanged(null, new EventArgsValues <PowerScheme>(oldPlan, newPlan)); } oldStaticValues[EventType.PowerSchemeChanged] = newPlan; break; } }
static void Main(string[] args) { IntPtr activeGuidPtr = IntPtr.Zero; //uint res = PowerScheme.PowerGetActiveScheme(IntPtr.Zero, ref activeGuidPtr); //if (res == 0) //{ Guid VideoSettingGuid = new Guid(); UInt32 index = 0; UInt32 BufferSize = (UInt32)Marshal.SizeOf(typeof(Guid)); while (0 == PowerScheme.PowerEnumerate( IntPtr.Zero, activeGuidPtr, new Guid("7516b95f-f776-4464-8c53-06167f40cc99"), 18, index, ref VideoSettingGuid, ref BufferSize)) { Console.Write(VideoSettingGuid.ToString() + ": "); UInt32 size = 1024; IntPtr temp = Marshal.AllocHGlobal(1024); IntPtr type = IntPtr.Zero; PowerScheme.PowerReadACValue(IntPtr.Zero, activeGuidPtr, IntPtr.Zero, VideoSettingGuid, ref type, ref temp, ref size); Console.Write(Marshal.PtrToStringUni(temp)); Marshal.FreeHGlobal(temp); index++; } //} Console.ReadKey(); }
public void Dispose() { if (_oldPowerSheme != null) { SetActivePowerScheme(_oldPowerSheme.Guid); _oldPowerSheme = null; } }
private void UpdateIconText(PowerScheme activeScheme) { Icon.Text = activeScheme.Name; if (_monitorCheckWorker.AutoEnabled) { Icon.Text = Icon.Text + " (Auto)"; } }
public static PowerScheme GetActivePowerScheme() { PowerScheme result = GetPowerSchemes().FirstOrDefault(powerScheme => powerScheme.IsActive()); if (result == null) { throw new InvalidOperationException("Could not determine the active power scheme"); } return(result); }
public HighPerformancePowerScheme() { try { var powerSchemes = HighPerformancePowerScheme.GetPowerSchemes(); var oldPowerScheme = powerSchemes.FirstOrDefault(s => s.IsActive); var newPowerScheme = powerSchemes.FirstOrDefault(s => s.Guid == new Guid("8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c")); if (newPowerScheme != null && oldPowerScheme != null && oldPowerScheme != newPowerScheme) { SetActivePowerScheme(newPowerScheme.Guid); _oldPowerSheme = oldPowerScheme; } } catch { } }
public SchemeNode(Guid schemeGuid, PersistentSettings settings) : base() { this.settings = settings; this.powerScheme = new PowerScheme(); this.powerScheme.Guid = schemeGuid.ToString(); IntPtr ptrToPowerSchemeGuid = IntPtr.Zero; IntPtr friendlyName = IntPtr.Zero; uint buffSize = 1000; try { friendlyName = Marshal.AllocHGlobal((int)buffSize); ptrToPowerSchemeGuid = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Guid))); Marshal.StructureToPtr(schemeGuid, ptrToPowerSchemeGuid, true); //Pass the guid retrieved in PowerEnumerate as parameter //to get the power scheme name. NativeMethods.PowerReadFriendlyName( IntPtr.Zero, ptrToPowerSchemeGuid, IntPtr.Zero, IntPtr.Zero, friendlyName, ref buffSize); this.powerScheme.Name = Marshal.PtrToStringUni(friendlyName); this.powerScheme.Description = PowerManager.GetDescription(powerScheme.Guid, null, null); } catch (Exception exception) { throw new PowerManagerException(exception.Message, exception); } finally { Marshal.FreeHGlobal(ptrToPowerSchemeGuid); Marshal.FreeHGlobal(friendlyName); } }
public ProcessorStatePowerMode() { _powerScheme = PowerSchemesFactory.GetActivePowerScheme(); _originalMaxCPUStateSet = _powerScheme.GetMaxCPUState(); _originalMinCPUStateSet = _powerScheme.GetMinCPUState(); }
public PowerManager() { _powerSource = PowerSource.AC; _powerScheme = PowerScheme.HighPerformance; _batteryLife = 100; }
public Form1() { InitializeComponent(); family = K10Manager.GetFamily(); numPstates = K10Manager.GetHighestPState(); clock = K10Manager.GetBIOSBusSpeed(); //Brazos merge next line removed in BT //numBoostedPstates = K10Manager.GetNumBoostedStates(); numBoostedPstates = 0; processBarSteps = numPstates + numBoostedPstates + 1; processBarPerc = 100 / processBarSteps; if ((family != 12) && (family != 14) && (family != 16)) { MessageBox.Show("Your CPU/APU from AMD family: " + family + "h is not supported!"); } //needed to reduces flickering SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); if (numCores == 3) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); ShiftTable(-15); } else if (numCores == 2) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); this.Controls.Remove(this.pstateLabel3); this.Controls.Remove(this.core3label); this.Controls.Remove(this.cpu3Bar); ShiftTable(-30); } else if (numCores == 1) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); this.Controls.Remove(this.pstateLabel3); this.Controls.Remove(this.core3label); this.Controls.Remove(this.cpu3Bar); this.Controls.Remove(this.pstateLabel2); this.Controls.Remove(this.core2label); this.Controls.Remove(this.cpu2Bar); ShiftTable(-50); } notifyIcon.Icon = this.Icon; notifyIcon.ContextMenuStrip = new ContextMenuStrip(); notifyIcon.Visible = true; if (family == 16) { //MessageBox.Show("Jetzt wird ein Log für den Editor erstellt!"); //log_now(); } //Brazos merge next line was active in BT //this.Width += p0StateControl.GetDeltaOptimalWidth(); //Brazos merge p3 trough p7 inactive in BT //BT also provides integer value to Load for PState, which shouldn't be needed //MessageBox.Show("Jetzt werden die Register der GPU gelesen!"); nbp0StateControl.LoadFromHardware(); nbp1StateControl.LoadFromHardware(); //MessageBox.Show("Jetzt werden zusätzliche Register gelesen!"); statusinfo.LoadFromHardware(); //MessageBox.Show("Jetzt werden die Register der CPU gelesen!"); p0StateControl.LoadFromHardware(); p1StateControl.LoadFromHardware(); p2StateControl.LoadFromHardware(); p3StateControl.LoadFromHardware(); p4StateControl.LoadFromHardware(); p5StateControl.LoadFromHardware(); p6StateControl.LoadFromHardware(); p7StateControl.LoadFromHardware(); //MessageBox.Show("Alle Register gelesen!"); if (!_useWindowsPowerSchemes) { // use FusionTweaker's power schemes (via the registry) powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "Balanced" }); powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "High performance" }); powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "Power saver" }); var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"Software\FusionTweaker"); if (key == null) powerSchemesComboBox.SelectedIndex = 0; else { powerSchemesComboBox.SelectedIndex = (int)key.GetValue("PowerScheme", 0); key.Close(); } InitializeNotifyIconContextMenu(); powerSchemesComboBox.SelectedIndexChanged += (s, e) => { var k = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"Software\FusionTweaker"); k.SetValue("PowerScheme", powerSchemesComboBox.SelectedIndex); k.Close(); SynchronizeNotifyIconContextMenu(); }; return; } int guidSize = 16; IntPtr guid = Marshal.AllocHGlobal(guidSize); // get the GUID of the current power scheme IntPtr activeGuidPointer; if (PowerGetActiveScheme(IntPtr.Zero, out activeGuidPointer) != 0) throw new Exception("PowerGetActiveScheme()"); Guid activeGuid = (Guid)Marshal.PtrToStructure(activeGuidPointer, typeof(Guid)); LocalFree(activeGuidPointer); // iterate over all power schemes for (int i = 0; true; i++) { if (PowerEnumerate(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0x10, i, guid, ref guidSize) != 0) break; // get the required buffer size int size = 0; if (PowerReadFriendlyName(IntPtr.Zero, guid, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref size) != 0) break;//throw new Exception("PowerReadFriendlyName()"); IntPtr stringBuffer = Marshal.AllocHGlobal(size); // get the scheme name if (PowerReadFriendlyName(IntPtr.Zero, guid, IntPtr.Zero, IntPtr.Zero, stringBuffer, ref size) != 0) throw new Exception("PowerReadFriendlyName()"); var item = new PowerScheme() { Guid = (Guid)Marshal.PtrToStructure(guid, typeof(Guid)), Name = Marshal.PtrToStringUni(stringBuffer) }; Marshal.FreeHGlobal(stringBuffer); powerSchemesComboBox.Items.Add(item); if (item.Guid == activeGuid) powerSchemesComboBox.SelectedIndex = i; } Marshal.FreeHGlobal(guid); InitializeNotifyIconContextMenu(); powerSchemesComboBox.SelectedIndexChanged += (s, e) => { var item = (PowerScheme)powerSchemesComboBox.SelectedItem; if (PowerSetActiveScheme(IntPtr.Zero, ref item.Guid) != 0) throw new Exception("PowerSetActiveScheme()"); SynchronizeNotifyIconContextMenu(); }; }
private void UpdateIconImage(PowerScheme activeScheme) { var icon = _iconSettings.GetIconById(activeScheme.Id); Icon.Icon = icon != null ? new Icon(icon) : Resources.Icon1; }
public CpuUsageTargetedPowerMode(PowerScheme powerScheme) { _powerScheme = powerScheme; }
public Form1() { InitializeComponent(); if (family != 14) { MessageBox.Show("Your CPU/APU from AMD family: " + family + "h is not supported!"); } //needed to reduces flickering SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); if (numCores == 3) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); ShiftTable(-15); } else if (numCores == 2) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); this.Controls.Remove(this.pstateLabel3); this.Controls.Remove(this.core3label); this.Controls.Remove(this.cpu3Bar); ShiftTable(-30); } else if (numCores == 1) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); this.Controls.Remove(this.pstateLabel3); this.Controls.Remove(this.core3label); this.Controls.Remove(this.cpu3Bar); this.Controls.Remove(this.pstateLabel2); this.Controls.Remove(this.core2label); this.Controls.Remove(this.cpu2Bar); ShiftTable(-50); } notifyIcon.Icon = this.Icon; notifyIcon.ContextMenuStrip = new ContextMenuStrip(); notifyIcon.Visible = true; this.Width += p0StateControl.GetDeltaOptimalWidth(); p0StateControl.LoadFromHardware(0); p1StateControl.LoadFromHardware(1); p2StateControl.LoadFromHardware(2); nbp0StateControl.LoadFromHardware(3); nbp1StateControl.LoadFromHardware(4); statusinfo.LoadFromHardware(); if (!_useWindowsPowerSchemes) { // use BrazosTweaker's power schemes (via the registry) powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "Balanced" }); powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "High performance" }); powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "Power saver" }); var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"Software\BrazosTweaker"); if (key == null) powerSchemesComboBox.SelectedIndex = 0; else { powerSchemesComboBox.SelectedIndex = (int)key.GetValue("PowerScheme", 0); key.Close(); } InitializeNotifyIconContextMenu(); powerSchemesComboBox.SelectedIndexChanged += (s, e) => { var k = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"Software\BrazosTweaker"); k.SetValue("PowerScheme", powerSchemesComboBox.SelectedIndex); k.Close(); SynchronizeNotifyIconContextMenu(); }; return; } int guidSize = 16; IntPtr guid = Marshal.AllocHGlobal(guidSize); // get the GUID of the current power scheme IntPtr activeGuidPointer; if (PowerGetActiveScheme(IntPtr.Zero, out activeGuidPointer) != 0) throw new Exception("PowerGetActiveScheme()"); Guid activeGuid = (Guid)Marshal.PtrToStructure(activeGuidPointer, typeof(Guid)); LocalFree(activeGuidPointer); // iterate over all power schemes for (int i = 0; true; i++) { if (PowerEnumerate(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0x10, i, guid, ref guidSize) != 0) break; // get the required buffer size int size = 0; if (PowerReadFriendlyName(IntPtr.Zero, guid, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref size) != 0) break;//throw new Exception("PowerReadFriendlyName()"); IntPtr stringBuffer = Marshal.AllocHGlobal(size); // get the scheme name if (PowerReadFriendlyName(IntPtr.Zero, guid, IntPtr.Zero, IntPtr.Zero, stringBuffer, ref size) != 0) throw new Exception("PowerReadFriendlyName()"); var item = new PowerScheme() { Guid = (Guid)Marshal.PtrToStructure(guid, typeof(Guid)), Name = Marshal.PtrToStringUni(stringBuffer) }; Marshal.FreeHGlobal(stringBuffer); powerSchemesComboBox.Items.Add(item); if (item.Guid == activeGuid) powerSchemesComboBox.SelectedIndex = i; } Marshal.FreeHGlobal(guid); InitializeNotifyIconContextMenu(); powerSchemesComboBox.SelectedIndexChanged += (s, e) => { var item = (PowerScheme)powerSchemesComboBox.SelectedItem; if (PowerSetActiveScheme(IntPtr.Zero, ref item.Guid) != 0) throw new Exception("PowerSetActiveScheme()"); SynchronizeNotifyIconContextMenu(); }; }
public Form1() { InitializeComponent(); family = K10Manager.GetFamily(); numPstates = K10Manager.GetHighestPState(); clock = K10Manager.GetBIOSBusSpeed(); //Brazos merge next line removed in BT //numBoostedPstates = K10Manager.GetNumBoostedStates(); numBoostedPstates = 0; processBarSteps = numPstates + numBoostedPstates + 1; processBarPerc = 100 / processBarSteps; if ((family != 12) && (family != 14) && (family != 16)) { MessageBox.Show("Your CPU/APU from AMD family: " + family + "h is not supported!"); } //needed to reduces flickering SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); if (numCores == 3) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); ShiftTable(-15); } else if (numCores == 2) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); this.Controls.Remove(this.pstateLabel3); this.Controls.Remove(this.core3label); this.Controls.Remove(this.cpu3Bar); ShiftTable(-30); } else if (numCores == 1) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); this.Controls.Remove(this.pstateLabel3); this.Controls.Remove(this.core3label); this.Controls.Remove(this.cpu3Bar); this.Controls.Remove(this.pstateLabel2); this.Controls.Remove(this.core2label); this.Controls.Remove(this.cpu2Bar); ShiftTable(-50); } notifyIcon.Icon = this.Icon; notifyIcon.ContextMenuStrip = new ContextMenuStrip(); notifyIcon.Visible = true; if (family == 16) { //MessageBox.Show("Jetzt wird ein Log für den Editor erstellt!"); //log_now(); } //Brazos merge next line was active in BT //this.Width += p0StateControl.GetDeltaOptimalWidth(); //Brazos merge p3 trough p7 inactive in BT //BT also provides integer value to Load for PState, which shouldn't be needed //MessageBox.Show("Jetzt werden die Register der GPU gelesen!"); nbp0StateControl.LoadFromHardware(); nbp1StateControl.LoadFromHardware(); //MessageBox.Show("Jetzt werden zusätzliche Register gelesen!"); statusinfo.LoadFromHardware(); //MessageBox.Show("Jetzt werden die Register der CPU gelesen!"); p0StateControl.LoadFromHardware(); p1StateControl.LoadFromHardware(); p2StateControl.LoadFromHardware(); p3StateControl.LoadFromHardware(); p4StateControl.LoadFromHardware(); p5StateControl.LoadFromHardware(); p6StateControl.LoadFromHardware(); p7StateControl.LoadFromHardware(); //MessageBox.Show("Alle Register gelesen!"); if (!_useWindowsPowerSchemes) { // use FusionTweaker's power schemes (via the registry) powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "Balanced" }); powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "High performance" }); powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "Power saver" }); var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"Software\FusionTweaker"); if (key == null) { powerSchemesComboBox.SelectedIndex = 0; } else { powerSchemesComboBox.SelectedIndex = (int)key.GetValue("PowerScheme", 0); key.Close(); } InitializeNotifyIconContextMenu(); powerSchemesComboBox.SelectedIndexChanged += (s, e) => { var k = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"Software\FusionTweaker"); k.SetValue("PowerScheme", powerSchemesComboBox.SelectedIndex); k.Close(); SynchronizeNotifyIconContextMenu(); }; return; } int guidSize = 16; IntPtr guid = Marshal.AllocHGlobal(guidSize); // get the GUID of the current power scheme IntPtr activeGuidPointer; if (PowerGetActiveScheme(IntPtr.Zero, out activeGuidPointer) != 0) { throw new Exception("PowerGetActiveScheme()"); } Guid activeGuid = (Guid)Marshal.PtrToStructure(activeGuidPointer, typeof(Guid)); LocalFree(activeGuidPointer); // iterate over all power schemes for (int i = 0; true; i++) { if (PowerEnumerate(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0x10, i, guid, ref guidSize) != 0) { break; } // get the required buffer size int size = 0; if (PowerReadFriendlyName(IntPtr.Zero, guid, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref size) != 0) { break;//throw new Exception("PowerReadFriendlyName()"); } IntPtr stringBuffer = Marshal.AllocHGlobal(size); // get the scheme name if (PowerReadFriendlyName(IntPtr.Zero, guid, IntPtr.Zero, IntPtr.Zero, stringBuffer, ref size) != 0) { throw new Exception("PowerReadFriendlyName()"); } var item = new PowerScheme() { Guid = (Guid)Marshal.PtrToStructure(guid, typeof(Guid)), Name = Marshal.PtrToStringUni(stringBuffer) }; Marshal.FreeHGlobal(stringBuffer); powerSchemesComboBox.Items.Add(item); if (item.Guid == activeGuid) { powerSchemesComboBox.SelectedIndex = i; } } Marshal.FreeHGlobal(guid); InitializeNotifyIconContextMenu(); powerSchemesComboBox.SelectedIndexChanged += (s, e) => { var item = (PowerScheme)powerSchemesComboBox.SelectedItem; if (PowerSetActiveScheme(IntPtr.Zero, ref item.Guid) != 0) { throw new Exception("PowerSetActiveScheme()"); } SynchronizeNotifyIconContextMenu(); }; }
public Form1() { InitializeComponent(); if (family != 14) { MessageBox.Show("Your CPU/APU from AMD family: " + family + "h is not supported!"); } //needed to reduces flickering SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); if (numCores == 3) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); ShiftTable(-15); } else if (numCores == 2) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); this.Controls.Remove(this.pstateLabel3); this.Controls.Remove(this.core3label); this.Controls.Remove(this.cpu3Bar); ShiftTable(-30); } else if (numCores == 1) { this.Controls.Remove(this.pstateLabel4); this.Controls.Remove(this.core4label); this.Controls.Remove(this.cpu4Bar); this.Controls.Remove(this.pstateLabel3); this.Controls.Remove(this.core3label); this.Controls.Remove(this.cpu3Bar); this.Controls.Remove(this.pstateLabel2); this.Controls.Remove(this.core2label); this.Controls.Remove(this.cpu2Bar); ShiftTable(-50); } notifyIcon.Icon = this.Icon; notifyIcon.ContextMenuStrip = new ContextMenuStrip(); notifyIcon.Visible = true; this.Width += p0StateControl.GetDeltaOptimalWidth(); p0StateControl.LoadFromHardware(0); p1StateControl.LoadFromHardware(1); p2StateControl.LoadFromHardware(2); nbp0StateControl.LoadFromHardware(3); nbp1StateControl.LoadFromHardware(4); statusinfo.LoadFromHardware(); if (!_useWindowsPowerSchemes) { // use BrazosTweaker's power schemes (via the registry) powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "Balanced" }); powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "High performance" }); powerSchemesComboBox.Items.Add(new PowerScheme() { Name = "Power saver" }); var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"Software\BrazosTweaker"); if (key == null) { powerSchemesComboBox.SelectedIndex = 0; } else { powerSchemesComboBox.SelectedIndex = (int)key.GetValue("PowerScheme", 0); key.Close(); } InitializeNotifyIconContextMenu(); powerSchemesComboBox.SelectedIndexChanged += (s, e) => { var k = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"Software\BrazosTweaker"); k.SetValue("PowerScheme", powerSchemesComboBox.SelectedIndex); k.Close(); SynchronizeNotifyIconContextMenu(); }; return; } int guidSize = 16; IntPtr guid = Marshal.AllocHGlobal(guidSize); // get the GUID of the current power scheme IntPtr activeGuidPointer; if (PowerGetActiveScheme(IntPtr.Zero, out activeGuidPointer) != 0) { throw new Exception("PowerGetActiveScheme()"); } Guid activeGuid = (Guid)Marshal.PtrToStructure(activeGuidPointer, typeof(Guid)); LocalFree(activeGuidPointer); // iterate over all power schemes for (int i = 0; true; i++) { if (PowerEnumerate(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0x10, i, guid, ref guidSize) != 0) { break; } // get the required buffer size int size = 0; if (PowerReadFriendlyName(IntPtr.Zero, guid, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref size) != 0) { break;//throw new Exception("PowerReadFriendlyName()"); } IntPtr stringBuffer = Marshal.AllocHGlobal(size); // get the scheme name if (PowerReadFriendlyName(IntPtr.Zero, guid, IntPtr.Zero, IntPtr.Zero, stringBuffer, ref size) != 0) { throw new Exception("PowerReadFriendlyName()"); } var item = new PowerScheme() { Guid = (Guid)Marshal.PtrToStructure(guid, typeof(Guid)), Name = Marshal.PtrToStringUni(stringBuffer) }; Marshal.FreeHGlobal(stringBuffer); powerSchemesComboBox.Items.Add(item); if (item.Guid == activeGuid) { powerSchemesComboBox.SelectedIndex = i; } } Marshal.FreeHGlobal(guid); InitializeNotifyIconContextMenu(); powerSchemesComboBox.SelectedIndexChanged += (s, e) => { var item = (PowerScheme)powerSchemesComboBox.SelectedItem; if (PowerSetActiveScheme(IntPtr.Zero, ref item.Guid) != 0) { throw new Exception("PowerSetActiveScheme()"); } SynchronizeNotifyIconContextMenu(); }; }