Question

I'm trying to determine if a laptop is connected to AC power.

The OS Im running under is WinPE. My app is written in native C++.

WMI queries using Win32_Battery are not supported and the GetSystemPowerStatus API always returns '1' for ACLineStatus (running on AC power or not).

Any ideas?

Additonal investigation: Just tried the API 'CallNtPowerInformation' with POWER_INFORMATION_LEVEL::SystemBatteryState. The SYSTEM_BATTERY_STATUS structure element AcOnLine also returns 1 regardless of power supply status. Probably just calls the same system level code but thought I'd add it in here.

Was it helpful?

Solution

I managed to answer my own question and it proved to be very simple in the end.

In WinPE the following noddy script returned null when executed because the battery wasn't being recognised:

strComputer = "." Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\CIMV2") Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Battery",,48) For Each objItem in colItems Wscript.Echo objItem.BatteryStatus Wscript.Echo objItem.Caption Next

I found a battery device driver in my PE image (\windows\inf\battery.inf) which once installed resulted in the battery being recognised and the above script returning the expected values. i.e. BatteryStatus = 2 (The system has access to AC so no battery is being discharged) or BatteryStatus = 1 (The battery is discharging i.e. AC not connected).

Driver can be installed in the PE image itself or loaded on demand. i.e. drvload

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top