Hi all,
Just wondering if anyone has some code to read the battery level (%) of a laptop or tablet under windows?
Thanks heaps!
James
Hi all,
Just wondering if anyone has some code to read the battery level (%) of a laptop or tablet under windows?
Thanks heaps!
James
I’d hazard a guess you need a declare to
https://msdn.microsoft.com/en-us/library/windows/desktop/aa372693(v=vs.85).aspx
BOOL WINAPI GetSystemPowerStatus( Out LPSYSTEM_POWER_STATUS lpSystemPowerStatus );
https://msdn.microsoft.com/en-us/library/windows/desktop/aa373232(v=vs.85).aspx
typedef struct _SYSTEM_POWER_STATUS {
BYTE ACLineStatus;
BYTE BatteryFlag;
BYTE BatteryLifePercent;
BYTE Reserved1;
DWORD BatteryLifeTime;
DWORD BatteryFullLifeTime;
} SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS;
Structure SystemPowerStatus
aclinestatus as byte
batteryflag as byte
batterylifepercent As byte
reserved1 As byte
batterylifetime as int32
batteryfulllifetime As int32
end structure
[code]soft declare GetSystemPowerStatus lib “Kernel32” (byref inout as systempowerstatus ) as integer
dim status as systempowerstatus
dim result as integer
result = GetSystemPowerStatus(status)
// if result = 0 then call failed
// result <> 0 call suceeded[/code]
Maybe this one work for you.
// INFO:
// http://library.wmifun.net/cimv2/win32_battery.html
//
//https://msdn.microsoft.com/en-us/library/aa394074%28v=vs.85%29.aspx
Dim locator, objWMIService, objs, objProperty As OLEOBJECT
Dim nobjs as Integer
// Connect to WMI
locator = new oleObject("WbemScripting.SWbemlocator", true)
Dim wmiServiceParams(2) as variant
wmiServiceParams(1) = "."
wmiServiceParams(2) = "root\\cimv2"
objWMIService= locator.invoke("ConnectServer", wmiServiceParams)
// Run the WMI query
objs = objWMIService.ExecQuery ("SELECT * FROM Win32_Battery")
nobjs = objs.count - 1
//msgbox ("Number of Objects: " + str(nobjs +1))
For i as integer = 0 to nobjs
Dim stringData As String
objProperty = objs.ItemIndex(i)
// ItemIndex() is not supported in Windows XP only from Windows Vista and upwards
stringData = "Availability: " + objProperty.Value("Availability") + EndOfLine _
+ "BatteryRechargeTimel: " + objProperty.Value("BatteryRechargeTime") + EndOfLine _
+ "BatteryStatus: " + objProperty.Value("BatteryStatus") + EndOfLine _
+"Caption: " + objProperty.Value("Caption") + EndOfLine _
+ "Chemistry: " + objProperty.Value("Chemistry") + EndOfLine _
+ "ConfigManagerErrorCode: " + objProperty.Value("ConfigManagerErrorCode") + EndOfLine _
+ "ConfigManagerUserConfig: " + objProperty.Value("ConfigManagerUserConfig") + EndOfLine _
+ "CreationClassName: " + objProperty.Value("CreationClassName") + EndOfLine _
+ "Description: " + objProperty.Value("Description") + EndOfLine _
+ "DesignCapacity: " + objProperty.Value("DesignCapacity") + EndOfLine _
+ "DesignVoltage: " + objProperty.Value("DesignVoltage") + EndOfLine _
+ "DeviceID: " + objProperty.Value("DeviceID") + EndOfLine _
+ "ErrorCleared: " + objProperty.Value("ErrorCleared") + EndOfLine _
+ "ErrorDescriptionr: " + objProperty.Value("ErrorDescription") + EndOfLine _
+ "EstimatedChargeRemaining: " + objProperty.Value("EstimatedChargeRemaining") + EndOfLine _
+ "EstimatedRunTime: " + objProperty.Value("EstimatedRunTime") + EndOfLine _
+ "ExpectedBatteryLife: " + objProperty.Value("ExpectedBatteryLife") + EndOfLine _
+ "ExpectedLife: " + objProperty.Value("ExpectedLife") + EndOfLine _
+ "FullChargeCapacity: " + objProperty.Value("FullChargeCapacity") + EndOfLine _
+ "InstallDate: " + objProperty.Value("InstallDate") + EndOfLine _
+ "LastErrorCode: " + objProperty.Value("LastErrorCode") + EndOfLine _
+ "MaxRechargeTime: " + objProperty.Value("MaxRechargeTime") + EndOfLine _
+ "Name: " + objProperty.Value("Name") + EndOfLine _
+ "PNPDeviceID: " + objProperty.Value("PNPDeviceID") + EndOfLine
msgbox stringData
Next
locator = Nil
exception err as oleexception
msgbox err.message
Thanks very much guys. I’ve sorted it out… I’ll post code shortly when I’m back at work!