Skip to content

Instantly share code, notes, and snippets.

@ov3rk1ll
Created July 8, 2014 14:43
Show Gist options
  • Select an option

  • Save ov3rk1ll/d3e5cec797595dc8ec5b to your computer and use it in GitHub Desktop.

Select an option

Save ov3rk1ll/d3e5cec797595dc8ec5b to your computer and use it in GitHub Desktop.
WM6 GetBatteryLifePercent
[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