J
Jim Wilson [eMVP]
You'll need the GetSystemPowerStatusEx Win32 method.
Here's the DllImport and a C# method that calls it returning the % remaining
and whether the device is currently plugged in
// import GetSystemPowerStatusEx
[DllImport("coredll.dll")]
private static extern int GetSystemPowerStatusEx(System_Power_Status_Ex
status, int update) ;
//GetPowerInfo wrapper method
// parameters: ref bool returning whether the device is plugged in
// ref int indicating remaining battery percentage
public static void GetPowerInfo(ref bool pluggedIn, ref int percentBattery)
{
System_Power_Status_Ex powerStat = new System_Power_Status_Ex() ;
GetSystemPowerStatusEx(powerStat, 1);
pluggedIn = powerStat.ACLineStatus == 1 ;
percentBattery = powerStat.BatteryLifePercent;
}
Here's the definition of the System_Power_Status_Ex structure that the
GetSystemPowerStatusEx needs.
// Define SYSTEM_POWER_STATUS_EX
internal class System_Power_Status_Ex
{
internal System_Power_Status_Ex()
{
ACLineStatus = 0;
BatteryFlag = 0;
BatteryLifePercent = 0;
Reserved1 = 0;
BatteryLifeTime = 0;
BatteryFullLifeTime = 0;
Reserved2 = 0;
BackupBatteryFlag = 0;
BackupBatteryLifePercent = 0;
Reserved3 = 0;
BackupBatteryLifeTime = 0;
BackupBatteryFullLifeTime = 0;
}
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;
}
Jim Wilson, eMVP
http://www.jwhh.com
http://www.develop.com
Here's the DllImport and a C# method that calls it returning the % remaining
and whether the device is currently plugged in
// import GetSystemPowerStatusEx
[DllImport("coredll.dll")]
private static extern int GetSystemPowerStatusEx(System_Power_Status_Ex
status, int update) ;
//GetPowerInfo wrapper method
// parameters: ref bool returning whether the device is plugged in
// ref int indicating remaining battery percentage
public static void GetPowerInfo(ref bool pluggedIn, ref int percentBattery)
{
System_Power_Status_Ex powerStat = new System_Power_Status_Ex() ;
GetSystemPowerStatusEx(powerStat, 1);
pluggedIn = powerStat.ACLineStatus == 1 ;
percentBattery = powerStat.BatteryLifePercent;
}
Here's the definition of the System_Power_Status_Ex structure that the
GetSystemPowerStatusEx needs.
// Define SYSTEM_POWER_STATUS_EX
internal class System_Power_Status_Ex
{
internal System_Power_Status_Ex()
{
ACLineStatus = 0;
BatteryFlag = 0;
BatteryLifePercent = 0;
Reserved1 = 0;
BatteryLifeTime = 0;
BatteryFullLifeTime = 0;
Reserved2 = 0;
BackupBatteryFlag = 0;
BackupBatteryLifePercent = 0;
Reserved3 = 0;
BackupBatteryLifeTime = 0;
BackupBatteryFullLifeTime = 0;
}
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;
}
Jim Wilson, eMVP
http://www.jwhh.com
http://www.develop.com