[C #] using GetSystemPowerStatus API to see the current state of the power use

[C #] using GetSystemPowerStatus API to see the current state of the power use


Introduction

This simply record what, how through GetSystemPowerStatus API, to view the current status of the power supply.

Library

kernel32.dll

GetSystemPowerStatus API

GetSystemPowerStatus API can get the state of the power system, including the use of AC or DC, whether the state of charge, the remaining usable time information. Its function prototype is as follows:
BOOL WINAPI GetSystemPowerStatus (__ OUT LPSYSTEM_POWER_STATUS lpSystemPowerStatus );

Configuration parameters passed LPSYSTEM_POWER_STATUS patterns
typedef struct {_SYSTEM_POWER_STATUS
BYTE ACLineStatus;
BYTE BatteryFlag;
BYTE BatteryLifePercent;
BYTE the Reserved1;
DWORD BatteryLifeTime;
DWORD BatteryFullLifeTime;} SYSTEM_POWER_STATUS,
* LPSYSTEM_POWER_STATUS;

 
Meaning structure members represented briefly as follows:
BatteryFlag It indicates the current state of charge.
1 is High (66% power), 2 Low (33% power), 4 Critical (5% or less power), 8 Charging, 128 is NoBattery, 255 is UnKnow
BatteryFullLifeTime It indicates how long full power may be used (-1 unknown).
BatteryLifePercent It represents the percentage power surplus, which is normal for the power rating of 0 to 100,255.
BatteryLifeTime It indicates how long the remaining power may be used (-1 unknown).
ACLineStatus It indicates power status.
0 is offline, 1 is online, 255 of unknow.
 
When using the structure as long as the incoming patterns LPSYSTEM_POWER_STATUS GetSystemPowerStatus, and then extracted from the incoming data of interest can be a structure.
 

Example

For ease of use, but also easy compared to controls. Here I GetSystemPowerStatus API package became similar System.Windows.Form.PowerStatus class.


using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Battery
{
    [Flags]
    public enum BatteryChargeStatus : byte
    {
        High = 1,
        Low = 2,
        Critical = 4,
        Charging = 8,
        NoSystemBattery = 128,
        Unknown = 255
    }

    public enum PowerLineStatus : byte
    {
        Offline = 0,
        Online = 1,
        Unknown = 255
    }

    class PowerStatus
    {
        [DllImport("kernel32", EntryPoint = "GetSystemPowerStatus")]
        private static extern void GetSystemPowerStatus(ref SystemPowerStatus powerStatus);
        
        private struct SystemPowerStatus
        {
            public PowerLineStatus PowerLineStatus;
            public BatteryChargeStatus BatteryChargeStatus;
            public Byte BatteryLifePercent;
            public Byte Reserved;
            public int BatteryLifeRemaining;
            public int BatteryFullLifeTime;
        }

        private SystemPowerStatus _powerStatus;

        public PowerLineStatus PowerLineStatus
        {
            get
            {
                return _powerStatus.PowerLineStatus;
            }
        }

        public BatteryChargeStatus BatteryChargeStatus
        {
            get
            {
                return _powerStatus.BatteryChargeStatus;
            }
        }

        public float BatteryLifePercent
        {
            get
            {
                return _powerStatus.BatteryLifePercent;
            }
        }

        public int BatteryLifeRemaining
        {
            get
            {
                return _powerStatus.BatteryLifeRemaining;
            }
        }

        public int BatteryFullLifeTime
        {
            get
            {
                return _powerStatus.BatteryFullLifeTime;
            }
        }

        public PowerStatus()
        {
            UpdatePowerInfo();
        }


        public void UpdatePowerInfo()
        {
            GetSystemPowerStatus(ref _powerStatus);
        }
    }
}

Use the built-in .NET with PowerStatus similar. As soon as a corresponding attribute value.


using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace BatteryPower
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        private void UpdateInfo()
        {
            Battery.PowerStatus power = new Battery.PowerStatus();
            this.tbxPowerStatus.Text = power.PowerLineStatus.ToString();
            this.tbxBatteryChargeStatus.Text = power.BatteryChargeStatus.ToString();
            this.tbxBatteryFullLifetime.Text = power.BatteryFullLifeTime.ToString();
            this.tbxBatteryLifePercent.Text = power.BatteryLifePercent.ToString();
            this.tbxBatteryLifeRemaining.Text = power.BatteryLifeRemaining.ToString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            UpdateInfo();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            UpdateInfo();
        }
    }
}

Execution screen

image

Link

  • GetSystemPowerStatus Function
  • SYSTEM_POWER_STATUS Structure
  • C # get NoteBook battery usage via Windows API
  • How do you know the battery usage Notebook

Original: Large column  [C #] using GetSystemPowerStatus API to see the current state of the power use


Guess you like

Origin www.cnblogs.com/chinatrump/p/11468544.html