Home > database >  How to get battery data in Inno Setup?
How to get battery data in Inno Setup?

Time:04-01

I understand it should be similar to the following code:

[Code]
type
  TSystemPowerStatus = {???};

function GetBattery(SYSTEM_POWER_STATUS: TSystemPowerStatus): Boolean;
external '[email protected] stdcall';

procedure ShowBatteryStatus();
var
  PowerStatus: TSystemPowerStatus; 
  State: String;
begin
  GetBattery(PowerStatus);
  State := IntToStr(PowerStatus.BatteryLifePercent);
  MsgBox(format('Battery Precents: %p',[State]), mbInformation, MB_OK);
end;

function InitializeSetup: Boolean;
begin
  ShowBatteryStatus()
  Result := True
end;

But I could not find the right type for TSystemPowerStatus.

The struct type is not recognized.

Can anyone help with this?

Thank you!

CodePudding user response:

This worked for me:

[Code]
type
  TSystemPowerStatus = record
    ACLineStatus : Byte;
    BatteryFlag : Byte;
    BatteryLifePercent : Byte;
    Reserved1 : Byte;
    BatteryLifeTime : DWORD;
    BatteryFullLifeTime : DWORD;
  end;

function GetSystemPowerStatus(var SYSTEM_POWER_STATUS: TSystemPowerStatus): Boolean;
external '[email protected] stdcall';

procedure ShowBatteryStatus();
var
  PowerStatus: TSystemPowerStatus; 
  State: String;

begin
  PowerStatus.ACLineStatus := 0;
  PowerStatus.BatteryFlag := 0;
  PowerStatus.BatteryLifePercent  := 0;
  PowerStatus.Reserved1 := 0;
  PowerStatus.BatteryLifeTime := 0;
  PowerStatus.BatteryFullLifeTime := 0;

  if (GetSystemPowerStatus(PowerStatus)) then
  begin
    State := IntToStr(PowerStatus.BatteryLifePercent);
    MsgBox(format('Battery Precents: %s',[State]), mbInformation, MB_OK);
  end
  else begin
    MsgBox(SysErrorMessage(DLLGetLastError), mbError, mb_Ok);    
  end;
end;

function InitializeSetup: Boolean;
begin
  ShowBatteryStatus()
  Result := True
end;
  • Related