SYSTEM_POWER_STATUS, BatteryFlag returns 9 when charging.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa373232(v=vs.85).aspx

1
2
3
4
5
6
if(msg->message == WM_POWERBROADCAST )
   {
       SYSTEM_POWER_STATUS pwr;
           GetSystemPowerStatus(&pwr);
           qDebug() << pwr.BatteryFlag << endl;
}


When discharging it returned 1, as i had more than 66% battery
When charging it returned 9. Isn't it supposed to return 8?
I only want to know if the battery is charging or discharging, should i use another funtion?

Windows 8.1, QT Creator 5.3
Last edited on
Re-read the description of BatteryFlag. It actually contains bit flags. The reason it is 9 is because both flags 8 and 1 are set(that's 8 OR 1). To see if charging check flag 8 only, e.g,

if (pwr.BatteryFlag & 8) ...
Not sure, but you mean that if i want to check if it's charging

if (pwr.BatteryFlag > 8 && pwr.BatteryFlag<13)

am i right?

Last edited on
No...

Check if charging:
1
2
3
4
if (pwr.BatteryFlag & 8)
{
      // charging
}

You should read up on bit flags and the bitwise operators. Here, for example:
http://www.cprogramming.com/tutorial/bitwise_operators.html
So the complete solution should be like this:?

1
2
3
4
5
6
7
8
9
10
11
SYSTEM_POWER_STATUS pwr;
GetSystemPowerStatus(&pwr);

if (pwr.BatteryFlag==255)
     //Unable to read
else if (pwr.BatteryFlag==128)
    //NO Battery
else if (pwr.BatteryFlag  & 8)
    //Charging
else
    //Discharging 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
SYSTEM_POWER_STATUS pwr;
GetSystemPowerStatus(&pwr);

if (pwr.BatteryFlag & 255)
     //Unable to read
else if (pwr.BatteryFlag & 128)
    //NO Battery
else if (pwr.BatteryFlag  & 8)
    //Charging
else
    //Discharging  
Registered users can post here. Sign in or register to post.