DelphiFAQ Home Search:

How to get the battery life

 

comments1 comments. Current rating: 3 stars (1 votes). Leave comments and/ or rate it.

Does your application need to check whether the computer is working off the battery and how much more battery life is left?
The API function GetSystemPowerStatus() provides this information.

unit BatteryTools;

interface

function GetBatteryFlag : integer;
function GetBatteryLife : integer;
function GetBatteryStatus : boolean;

implementation

uses
  Windows;

// Returns the battery charge status
// Results:
// 1 - High power
// 2 - Low power
// 4 - Critical
// 8 - Charging
// 128 - No status available
// 256 - Unknown status
function GetBatteryFlag : integer;
var
  Stat: TSystemPowerStatus;
begin { GetBatteryFlag }
  GetSystemPowerStatus(Stat);
  Result := Stat.BatteryFlag
end; { GetBatteryFlag }


// Returns the battery life in percent
function GetBatteryLife : integer;
var
  Stat: TSystemPowerStatus;
begin { GetBatteryLife }
  GetSystemPowerStatus(Stat);
  Result := Stat.BatteryLifePercent;
  if (Result<0)
      or
     (Result>100) then
    Result := -1
end; { GetBatteryLife }


// if the AC power status is online true is returned, if offline or unknown then false
function GetBatteryStatus : boolean;
var
  iTemp: integer;
  Stat: TSystemPowerStatus;
begin { GetBatteryStatus }
  GetSystemPowerStatus(Stat);
  iTemp := Stat.ACLineStatus;
  Result := iTemp=1
end; { GetBatteryStatus }


begin

end.
You don't like the formatting? Check out SourceCoder then!

Comments:

2006-01-10, 07:54:15
u10 from Ukraine  
rating
And if i have multiple batteries, can i get separate data for each of them?

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: