/// <summary> /// Gets the current power policy. /// </summary> /// <returns>A PowerPolicies struct representing the current power policy.</returns> public static PowerPolicies GetCurrentPowerPolicy() { GLOBAL_POWER_POLICY globalPowerPolicy = new GLOBAL_POWER_POLICY(); POWER_POLICY powerPolicy = new POWER_POLICY(); if (!NativeMethods.GetCurrentPowerPolicies(out globalPowerPolicy, out powerPolicy)) { var ex = Win32ExceptionManager.GetWin32Exception(); throw ex; } PowerPolicies ppRet = new PowerPolicies(); ppRet.GlobalPowerPolicy = globalPowerPolicy; ppRet.PowerPolicy = powerPolicy; return(ppRet); }
internal static extern bool GetCurrentPowerPolicies(out GLOBAL_POWER_POLICY pGlobalPowerPolicy, out POWER_POLICY pPowerPolicy);
static extern bool GetCurrentPowerPolicies([Out] GLOBAL_POWER_POLICY pGlobalPowerPolicy, [Out] POWER_POLICY pPowerPolicy);
private static extern bool SetActivePwrScheme(uint uiID, ref GLOBAL_POWER_POLICY lpGlobalPowerPolicy, ref POWER_POLICY lpPowerPolicy);
private static extern bool GetCurrentPowerPolicies(ref GLOBAL_POWER_POLICY pGlobalPowerPolicy, ref POWER_POLICY pPowerPolicy);
/// <summary> /// Set power policy setting value for indicated power status (Windows XP) /// </summary> /// <param name="settingType"></param> /// <param name="AC"></param> /// <param name="value"></param> private static void SetPowerPolicySetting(SystemPowerSettingType settingType, bool AC, uint value) { try { GLOBAL_POWER_POLICY gpp = new GLOBAL_POWER_POLICY(); POWER_POLICY pp = new POWER_POLICY(); UInt32 index = 0; POWER_ACTION action; PowerActionEventCode eventCode; if (!GetCurrentPowerPolicies(ref gpp, ref pp)) return; GetActivePwrScheme(ref index); switch (settingType) { case SystemPowerSettingType.STANDBYIDLE: if (AC) { pp.user.IdleTimeoutAc = value; if (value == 0 && pp.mach.DozeS4TimeoutAc != 0) pp.user.IdleAc.Action = POWER_ACTION.PowerActionHibernate; else pp.user.IdleAc.Action = POWER_ACTION.PowerActionSleep; } else { pp.user.IdleTimeoutDc = value; if (value == 0 && pp.mach.DozeS4TimeoutDc != 0) pp.user.IdleDc.Action = POWER_ACTION.PowerActionHibernate; else pp.user.IdleDc.Action = POWER_ACTION.PowerActionSleep; } break; case SystemPowerSettingType.HIBERNATE_AFTER: if (AC) { if (pp.user.IdleTimeoutAc == 0) { pp.user.IdleTimeoutAc = value; pp.mach.DozeS4TimeoutAc = 0; pp.user.IdleAc.Action = POWER_ACTION.PowerActionHibernate; } else { if (value > pp.user.IdleTimeoutAc) pp.mach.DozeS4TimeoutAc = value - pp.user.IdleTimeoutAc; else pp.mach.DozeS4TimeoutAc = value; pp.user.IdleAc.Action = POWER_ACTION.PowerActionSleep; } } else { if (pp.user.IdleTimeoutDc == 0) { pp.user.IdleTimeoutDc = value; pp.mach.DozeS4TimeoutDc = 0; pp.user.IdleDc.Action = POWER_ACTION.PowerActionHibernate; } else { if (value > pp.user.IdleTimeoutDc) pp.mach.DozeS4TimeoutDc = value - pp.user.IdleTimeoutDc; else pp.mach.DozeS4TimeoutDc = value; pp.user.IdleDc.Action = POWER_ACTION.PowerActionSleep; } } break; case SystemPowerSettingType.LID_CLOSE_ACTION: eventCode = 0; switch (value) { case 0: // Do nothing action = POWER_ACTION.PowerActionNone; eventCode = PowerActionEventCode.POWER_FORCE_TRIGGER_RESET; break; case 1: // Sleep action = POWER_ACTION.PowerActionSleep; break; case 2: // Hibernate action = POWER_ACTION.PowerActionHibernate; break; case 3: // Shutdown action = POWER_ACTION.PowerActionNone; eventCode = PowerActionEventCode.POWER_USER_NOTIFY_SHUTDOWN; break; case 4: // Ask User action = POWER_ACTION.PowerActionNone; eventCode = PowerActionEventCode.POWER_USER_NOTIFY_BUTTON; break; default: return; } if (AC) { gpp.user.LidCloseAc.Action = action; gpp.user.LidCloseAc.EventCode = eventCode; } else { gpp.user.LidCloseDc.Action = action; gpp.user.LidCloseDc.EventCode = eventCode; } break; case SystemPowerSettingType.POWER_BUTTON_ACTION: eventCode = 0; switch (value) { case 0: // Do nothing action = POWER_ACTION.PowerActionNone; eventCode = PowerActionEventCode.POWER_FORCE_TRIGGER_RESET; break; case 1: // Sleep action = POWER_ACTION.PowerActionSleep; break; case 2: // Hibernate action = POWER_ACTION.PowerActionHibernate; break; case 3: // Shutdown action = POWER_ACTION.PowerActionNone; eventCode = PowerActionEventCode.POWER_USER_NOTIFY_SHUTDOWN; break; case 4: // Ask User action = POWER_ACTION.PowerActionNone; eventCode = PowerActionEventCode.POWER_USER_NOTIFY_BUTTON; break; default: return; } if (AC) { gpp.user.PowerButtonAc.Action = action; gpp.user.PowerButtonAc.EventCode = eventCode; } else { gpp.user.PowerButtonDc.Action = action; gpp.user.PowerButtonDc.EventCode = eventCode; } break; case SystemPowerSettingType.SLEEP_BUTTON_ACTION: eventCode = 0; switch (value) { case 0: // Do nothing action = POWER_ACTION.PowerActionNone; eventCode = PowerActionEventCode.POWER_FORCE_TRIGGER_RESET; break; case 1: // Sleep action = POWER_ACTION.PowerActionSleep; break; case 2: // Hibernate action = POWER_ACTION.PowerActionHibernate; break; case 3: // Shutdown action = POWER_ACTION.PowerActionNone; eventCode = PowerActionEventCode.POWER_USER_NOTIFY_SHUTDOWN; break; case 4: // Ask User action = POWER_ACTION.PowerActionNone; eventCode = PowerActionEventCode.POWER_USER_NOTIFY_BUTTON; break; default: return; } if (AC) { gpp.user.SleepButtonAc.Action = action; gpp.user.SleepButtonAc.EventCode = eventCode; } else { gpp.user.SleepButtonDc.Action = action; gpp.user.SleepButtonDc.EventCode = eventCode; } break; default: return; } SetActivePwrScheme(index, ref gpp, ref pp); } catch (Exception) { } }
/// <summary> /// Get power policy setting for indicated power status (Windows XP) /// </summary> /// <param name="settingType">Returns value of power setting requested. Returns -1 on errors.</param> /// <returns></returns> private static int GetPowerPolicySetting(SystemPowerSettingType settingType, bool AC) { try { GLOBAL_POWER_POLICY gpp = new GLOBAL_POWER_POLICY(); POWER_POLICY pp = new POWER_POLICY(); POWER_ACTION action; PowerActionEventCode eventCode; if (!GetCurrentPowerPolicies(ref gpp, ref pp)) { return -1; } switch (settingType) { case SystemPowerSettingType.STANDBYIDLE: if (AC) { if (pp.user.IdleAc.Action == POWER_ACTION.PowerActionSleep) return (int)pp.user.IdleTimeoutAc; } else { if (pp.user.IdleDc.Action == POWER_ACTION.PowerActionSleep) return (int)pp.user.IdleTimeoutDc; } return 0; case SystemPowerSettingType.HIBERNATE_AFTER: if (AC) { if (pp.user.IdleAc.Action == POWER_ACTION.PowerActionHibernate) return (int)pp.user.IdleTimeoutAc; if (pp.mach.DozeS4TimeoutAc != 0) return (int)(pp.mach.DozeS4TimeoutAc + pp.user.IdleTimeoutAc); } else { if (pp.user.IdleDc.Action == POWER_ACTION.PowerActionHibernate) return (int)pp.user.IdleTimeoutDc; if (pp.mach.DozeS4TimeoutDc != 0) return (int)(pp.mach.DozeS4TimeoutDc + pp.user.IdleTimeoutDc); } return 0; case SystemPowerSettingType.LID_CLOSE_ACTION: if (AC) { action = gpp.user.LidCloseAc.Action; eventCode = gpp.user.LidCloseAc.EventCode; } else { action = gpp.user.LidCloseDc.Action; eventCode = gpp.user.LidCloseDc.EventCode; } switch (action) { case POWER_ACTION.PowerActionSleep: return 1; // Sleep case POWER_ACTION.PowerActionHibernate: return 2; // Hibernate default: if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_SHUTDOWN) != 0) return 3; // Shutdown if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_BUTTON) != 0) return 4; // Ask User return 0; //Do nothing } case SystemPowerSettingType.POWER_BUTTON_ACTION: if (AC) { action = gpp.user.PowerButtonAc.Action; eventCode = gpp.user.PowerButtonAc.EventCode; } else { action = gpp.user.PowerButtonDc.Action; eventCode = gpp.user.PowerButtonDc.EventCode; } switch (action) { case POWER_ACTION.PowerActionSleep: return 1; // Sleep case POWER_ACTION.PowerActionHibernate: return 2; // Hibernate default: if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_SHUTDOWN) != 0) return 3; // Shutdown if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_BUTTON) != 0) return 4; // Ask User return 0; //Do nothing } case SystemPowerSettingType.SLEEP_BUTTON_ACTION: if (AC) { action = gpp.user.SleepButtonAc.Action; eventCode = gpp.user.SleepButtonAc.EventCode; } else { action = gpp.user.SleepButtonDc.Action; eventCode = gpp.user.SleepButtonDc.EventCode; } switch (action) { case POWER_ACTION.PowerActionSleep: return 1; // Sleep case POWER_ACTION.PowerActionHibernate: return 2; // Hibernate default: if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_SHUTDOWN) != 0) return 3; // Shutdown if ((eventCode & PowerActionEventCode.POWER_USER_NOTIFY_BUTTON) != 0) return 4; // Ask User return 0; //Do nothing } default: return 0; } } catch (Exception) { return -1; } }