C# Signature:
[DllImport("kernel32.dll")]
static extern bool GetSystemPowerStatus(out SYSTEM_POWER_STATUS
lpSystemPowerStatus);
User-Defined Types:
Notes:
None.
Tips & Tricks:
Please add some!
Sample Code:
// C# Example by Matt Henry (henrym@sytexinc.com) //
using System;
using System.Runtime.InteropServices; // Necessary!
namespace MyNamespace
{
/// <summary>
/// Summary description for BatteryChecker.
/// </summary>
public class BatteryChecker
{
public BatteryChecker()
{
// Nothing
}
[DllImport("Kernel32")]
private static extern Boolean GetSystemPowerStatus( SystemPowerStatus sps );
public static SystemPowerStatus GetSystemPowerStatus()
{
SystemPowerStatus sps = new SystemPowerStatus();
GetSystemPowerStatus( sps );
return sps;
}
}
public enum ACLineStatus : byte
{
Offline = 0, Online = 1, Unknown = 255
}
public enum BatteryFlag : byte
{
High = 1,
Low = 2,
Critical = 4,
Charging = 8,
NoSystemBattery = 128,
Unknown = 255
}
// Fields must mirror their unmanaged counterparts, in order
[StructLayout( LayoutKind.Sequential )]
public class SystemPowerStatus
{
public ACLineStatus _ACLineStatus;
public BatteryFlag _BatteryFlag;
public Byte _BatteryLifePercent;
public Byte _Reserved1;
public Int32 _BatteryLifeTime;
public Int32 _BatteryFullLifeTime;
}
}
Sample Code:
// C# Example by Matt Wise (matt.wise@narfsoft.com) //
// There is not much functional diffrence between the above example, this would just be simpler class to implement.
using System;
using System.Runtime.InteropServices;
class SystemPower
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern Boolean GetSystemPowerStatus(out SystemPowerStatus sps);
private enum ACLineStatus : byte
{
Offline = 0,
Online = 1,
Unknown = 255
}
private enum BatteryFlag : byte
{
High = 1,
Low = 2,
Critical = 4,
Charging = 8,
NoSystemBattery = 128,
Unknown = 255
}
private struct SystemPowerStatus
{
public ACLineStatus LineStatus;
public BatteryFlag flgBattery;
public Byte BatteryLifePercent;
public Byte Reserved1;
public Int32 BatteryLifeTime;
public Int32 BatteryFullLifeTime;
}
/// <summary>
/// Returns true if a an AC Power Line is detected
/// </summary>
public static Boolean ACPowerPluggedIn()
{
SystemPowerStatus SPS = new SystemPowerStatus();
GetSystemPowerStatus(out SPS);
if (SPS.LineStatus == ACLineStatus.Online)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Returns an integer representing the percent of battery charge remaining.
/// </summary>
public static Int32 BatteryCharge()
{
SystemPowerStatus SPS = new SystemPowerStatus();
GetSystemPowerStatus(out SPS);
return (Int32)SPS.BatteryLifePercent;
}
}
Alternative Managed API:
System.Windows.Forms.PowerStatus
Please edit this page!
Do you have...
- helpful tips or sample code to share for using this API in managed code?
- corrections to the existing content?
- variations of the signature you want to share?
- additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).
GetSystemPowerStatus on MSDN
GetSystemPowerStatus on MSDNPlease edit this page!
Do you have...
- helpful tips or sample code to share for using this API in managed code?
- corrections to the existing content?
- variations of the signature you want to share?
- additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).