Monday, September 26, 2005

CF : Battery status in CF

bool success = GetSystemPowerStatusEx (ref system_power_status_ex, true);

this.lblMainInfo.Text = system_power_status_ex.BatteryLifePercent.ToString() +
"% Power Status " + (BatteryChargeStatusEnum)system_power_status_ex.BatteryFlag;

this.lblBackupInfo.Text = system_power_status_ex.BackupBatteryLifePercent.ToString() +
"% Power Status " + (BatteryChargeStatusEnum)system_power_status_ex.BatteryFlag;

where:

[DllImport("Coredll.dll")]
public static extern bool GetSystemPowerStatusEx(ref SYSTEM_POWER_STATUS_EX pstatus, bool fUpdate);

[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_POWER_STATUS_EX
{
public byte ACLineStatus;
public byte BatteryFlag;
public byte BatteryLifePercent;
public byte Reserved1;
public uint BatteryLifeTime;
public uint BatteryFullLifeTime;
public byte Reserved2;
public byte BackupBatteryFlag;
public byte BackupBatteryLifePercent;
public byte Reserved3;
public uint BackupBatteryLifeTime;
public uint BackupBatteryFullLifeTime;
}

private SYSTEM_POWER_STATUS_EX system_power_status_ex;

public enum BatteryChargeStatusEnum : byte
{
High = 1,
Low = 2,
Critical = 4,
Charging = 8,
NoSystemBattery = 128,
Unknown = 255
}

Thanks to the good folks at OpenNETCF (see "Links") for this.

Enjoy!

(Compact Framework)

No comments: