Export (0) Print
Expand All

HOWTO: Get the Device Power Status

.NET Compact Framework 1.0

Geoff Schwab
Excell Data Corporation

Contributions by Jonathan Wells
Microsoft Corporation

October, 2003

Applies to:
   Microsoft® .NET Compact Framework 1.0
   Microsoft Visual Studio® .NET 2003

Download sample.

Summary: This sample demonstrates how to P/Invoke the GetSystemPowerStatusEx and GetSystemPowerStatusEx2 functions, each of which fill a SYSTEM_POWER_STATUS_EX and SYSTEM_POWER_STATUS_EX2 class respectively. These classes contain information about the batteries and power status of the device.

Implementation

The power status functions each implement a different class which is passed to the function as the first parameter. Consult the Visual Studio .NET documentation for detailed descriptions of the members of these classes. These classes are defined in managed code below.

// C#
public class SYSTEM_POWER_STATUS_EX2
{
    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;
    public uint BatteryVoltage;
    public uint BatteryCurrent;
    public uint BatteryAverageCurrent;
    public uint BatteryAverageInterval;
    public uint BatterymAHourConsumed;
    public uint BatteryTemperature;
    public uint BackupBatteryVoltage;
   public byte BatteryChemistry;
}

public class 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;
}

' VB
Public Class SYSTEM_POWER_STATUS_EX2
    Public ACLineStatus As Byte
    Public BatteryFlag As Byte
    Public BatteryLifePercent As Byte
    Public Reserved1 As Byte
    Public BatteryLifeTime As System.UInt32
    Public BatteryFullLifeTime As System.UInt32
    Public Reserved2 As Byte
    Public BackupBatteryFlag As Byte
    Public BackupBatteryLifePercent As Byte
    Public Reserved3 As Byte
    Public BackupBatteryLifeTime As System.UInt32
    Public BackupBatteryFullLifeTime As System.UInt32
    Public BatteryVoltage As System.UInt32
    Public BatteryCurrent As System.UInt32
    Public BatteryAverageCurrent As System.UInt32
    Public BatteryAverageInterval As System.UInt32
    Public BatterymAHourConsumed As System.UInt32
    Public BatteryTemperature As System.UInt32
    Public BackupBatteryVoltage As System.UInt32
    Public BatteryChemistry As Byte
End Class 'SYSTEM_POWER_STATUS_EX2

Public Class SYSTEM_POWER_STATUS_EX
    Public ACLineStatus As Byte
    Public BatteryFlag As Byte
    Public BatteryLifePercent As Byte
    Public Reserved1 As Byte
    Public BatteryLifeTime As System.UInt32
    Public BatteryFullLifeTime As System.UInt32
    Public Reserved2 As Byte
    Public BackupBatteryFlag As Byte
    Public BackupBatteryLifePercent As Byte
    Public Reserved3 As Byte
    Public BackupBatteryLifeTime As System.UInt32
    Public BackupBatteryFullLifeTime As System.UInt32
End Class 'SYSTEM_POWER_STATUS_EX

The functions can be P/Invoked with the following declarations.

//C#
[DllImport("coredll")]
private static extern uint GetSystemPowerStatusEx(SYSTEM_POWER_STATUS_EX lpSystemPowerStatus,
    bool fUpdate);

[DllImport("coredll")]
private static extern uint GetSystemPowerStatusEx2(SYSTEM_POWER_STATUS_EX2 lpSystemPowerStatus,
    uint dwLen, bool fUpdate);

'VB
<DllImport("coredll")> _
Private Shared Function GetSystemPowerStatusEx( _
  ByVal lpSystemPowerStatus As SYSTEM_POWER_STATUS_EX, _
  ByVal fUpdate As Boolean) As System.UInt32
End Function


<DllImport("coredll")> _
Private Shared Function GetSystemPowerStatusEx2(ByVal _
  lpSystemPowerStatus As SYSTEM_POWER_STATUS_EX2, _
  ByVal dwLen As System.UInt32, ByVal fUpdate As Boolean) _
  As System.UInt32
End Function

The parameters and return values of the functions differ slightly. GetSystemPowerStatusEx returns 0 if it fails and a non-zero value otherwise. This function takes two parameters, an instance of a SYSTEM_POWER_STATUS_EX class and a bool which defines whether to query the hardware for the instant status or use a version of the power status cached by the OS. Alternatively, GetSystemPowerStatusEx2 returns the size of the SYSTEM_POWER_STATUS_EX2 class supplied as parameter 2 if successful and 0 otherwise. For its first parameter it takes an instance to a SYSTEM_POWER_STATUS_EX2 class, the second parameter is the size in bytes of this class, and the third parameter is a bool specifying whether to query the hardware for the update, the same as GetSystemPowerStatusEx's second parameter.

The following code demonstrates how to use these functions to update a label.

//C#
SYSTEM_POWER_STATUS_EX status = new SYSTEM_POWER_STATUS_EX();
SYSTEM_POWER_STATUS_EX2 status2 = new SYSTEM_POWER_STATUS_EX2();

if (GetSystemPowerStatusEx(status, false) == 1)
{
    this.label_main.Text = String.Format("{0}%",
        status.BatteryLifePercent);
}

if (GetSystemPowerStatusEx2(status2,
    (uint)Marshal.SizeOf(status2), false) ==
    (uint)Marshal.SizeOf(status2))
{
    this.label_backup.Text = String.Format("{0}%",
        status2.BackupBatteryLifePercent);
}

'VB
Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    
    Dim status As New SYSTEM_POWER_STATUS_EX
    Dim status2 As New SYSTEM_POWER_STATUS_EX2

    If Convert.ToInt32(GetSystemPowerStatusEx(status, False)) = 1 Then
        Me.label_main.Text = String.Format("{0}%", _
            status.BatteryLifePercent)
    End If

    If Convert.ToInt32(GetSystemPowerStatusEx2(status2, _
        Convert.ToUInt32(Marshal.SizeOf(status2)), False)) = _
        Marshal.SizeOf(status2) Then
        
        Me.label_backup.Text = String.Format("{0}%",
            status2.BackupBatteryLifePercent)
    End If
End Sub 'Form1_Load

Conclusion

The full sample provided with this document utilizes the code shown above to demonstrate how to detect the current battery life percentage of the main and backup batteries. The results are displayed in labels on a form.

Show:
© 2014 Microsoft