[DllImport("kernel32.dll")]
static extern bool GetSystemPowerStatus(out SYSTEM_POWER_STATUS
lpSystemPowerStatus);
None.
Please add some!
// C# Example by Matt Henry ([email protected]) //
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;
}
}
// C# Example by Matt Wise ([email protected]) //
// 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;
}
}
System.Windows.Forms.PowerStatus
Do you have...
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 MSDNDo you have...
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).