C get battery life under Windows 7

I try to encode a program that receives% of the laptop battery and then displays a CMD showing a message (for example: 10% β†’ "Low battery!"). I tried to do this and it seems like they all tried with C ++ or C #. Can someone help me with C, please?

Edit: thanks zakinster for your answer. Shouldn't it look like this? This code does not work.

#include <Windows.h>
#include <Winbase.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    SYSTEM_POWER_STATUS status;
    GetSystemPowerStatus(&status);
    unsigned char battery = status.BatteryLifePercent;
    printf("%s", battery);
}
+4
source share
1 answer

GetSystemPowerStatus from Win32, the API should provide the necessary information:

SYSTEM_POWER_STATUS status;
if(GetSystemPowerStatus(&status)) {
    unsigned char battery = status.BatteryLifePercent;
    /* battery := 0..100 or 255 if unknown */
    if(battery == 255) {
        printf("Battery level unknown !");
    }
    else {
        printf("Battery level : %u%%.", battery);
    }
} 
else {
    printf("Cannot get the power status, error %lu", GetLastError()); 
}

See the SYSTEM_POWER_STATUS structure documentation for a complete list of information .

+4

Source: https://habr.com/ru/post/1532538/


All Articles