Created
July 8, 2014 14:43
-
-
Save ov3rk1ll/d3e5cec797595dc8ec5b to your computer and use it in GitHub Desktop.
WM6 GetBatteryLifePercent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [DllImport("coredll.dll", SetLastError = true)] | |
| [return: MarshalAs(UnmanagedType.U4)] | |
| public static extern int GetSystemPowerStatusEx2(SYSTEM_POWER_STATUS_EX2 pSystemPowerStatusEx2, [MarshalAs(UnmanagedType.U4), In] int dwLen, [MarshalAs(UnmanagedType.Bool), In] bool fUpdate); | |
| // Percentage of full battery charge remaining. This member can be a value in the range 0 to 100, or BATTERY_PERCENTAGE_UNKNOWN if the status is unknown. | |
| private byte GetBatteryLifePercent() | |
| { | |
| SYSTEM_POWER_STATUS_EX2 SystemPowerStatusEx2 = new SYSTEM_POWER_STATUS_EX2(); | |
| if (GetSystemPowerStatusEx2(SystemPowerStatusEx2, Marshal.SizeOf(SystemPowerStatusEx2), true) > 0) { | |
| return SystemPowerStatusEx2.BatteryLifePercent; | |
| } else { | |
| return 0xFF; // BATTERY_PERCENTAGE_UNKNOWN Flag | |
| } | |
| } | |
| private bool m_bOpen = false; | |
| public class SYSTEM_POWER_STATUS_EX2 { | |
| public byte ACLineStatus; | |
| public byte BatteryFlag; | |
| public byte BatteryLifePercent; | |
| public byte Reserved1; | |
| public int BatteryLifeTime; | |
| public int BatteryFullLifeTime; | |
| public byte Reserved2; | |
| public byte BackupBatteryFlag; | |
| public byte BackupBatteryLifePercent; | |
| public byte Reserved3; | |
| public int BackupBatteryLifeTime; | |
| public int BackupBatteryFullLifeTime; | |
| public int BatteryVoltage; | |
| public int BatteryCurrent; | |
| public int BatteryAverageCurrent; | |
| public int BatteryAverageInterval; | |
| public int BatterymAHourConsumed; | |
| public int BatteryTemperature; | |
| public int BackupBatteryVoltage; | |
| public byte BatteryChemistry; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment