WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

System Info

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Check System Power Status

Keywords:  battery power laptop  GetSystemPowerStatus

Question:

Is there a way to tell is a laptop or notebook is running off a battery?

Answer:

This sample code, written for Windows 95, uses the DllCall function to interrogate the system power status. It can tell if a laptop or notebook computer is running off a battery or not, and if so, how much battery life is left.
;typedef struct _SYSTEM_POWER_STATUS {
;    BYTE ACLineStatus;           
;    BYTE  BatteryFlag;           
;    BYTE  BatteryLifePercent;    
;    BYTE  Reserved1;             
;    DWORD  BatteryLifeTime;      
;    DWORD  BatteryFullLifeTime;  
;} SYSTEM_POWER_STATUS;


buf=BinaryAlloc(100)
BinaryEODSet(buf,100)

dll=strcat(DirWindows(1),"KERNEL32.DLL")

rval=DllCall(dll,long:"GetSystemPowerStatus",lpbinary:buf)
if rval==0
   Message("Function Failed","Could not obtain power status")
else
   ;BinaryWrite(buf,"C:\temp\powerdebug.txt")
   ACLineStatus=BinaryPeek(buf,0)
   BatteryFlag=BinaryPeek(buf,1)
   BatLifePcnt=BinaryPeek(buf,2)
   BatRsvd=BinaryPeek(buf,3)
   BatLifeTime=BinaryPeek4(buf,4)
   BatFullTime=BinaryPeek4(buf,8)
   
   dline=""
   dline=strcat(dline,"ACLineStatus= ",ACLineStatus,@CRLF)
   dline=strcat(dline,"BatteryFlag= ",BatteryFlag,@CRLF)
   dline=strcat(dline,"BatLifePcnt= ",BatLifePcnt,@CRLF)
   dline=strcat(dline,"BatRsvd= ",BatRsvd,@CRLF)
   dline=strcat(dline,"BatLifeTime= ",BatLifeTime,@CRLF)
   dline=strcat(dline,"BatFullTime= ",BatFullTime,@CRLF)
   Message("Power Status is",dline)
   
endif

BinaryFree(buf)


Article ID:   W13138
Filename:   Check laptop battery status.txt
File Created: 2001:06:20:14:30:50
Last Updated: 2001:06:20:14:30:50