This should be in the Windows forum. You need to #include <Windows.h> for this...
Anyway, GetSystemPowerStatus() gets passed a LPSYSTEM_POWER_STATUS struct as an argument and returns true or false if it fails. You can follow the links and find:
which is what the LPSYSTEM_POWER_STATUS looks like.
so something like this:
1 2 3 4 5 6
LPSYSTEM_POWER_STATUS lpSysPwrStatus;
if (!GetSystemPowerStatus(lpSysPwrStatus)) returnfalse; // or return something meaningful
// you might need GetSystemPowerStatus(&lpSysPwrStatus)
// Battery life is now
lpSysPwrStatus.BatteryLifePercent
My link has more things that the LPSYSTEM_POWER_STATUS holds.
SYSTEM_POWER_STATUS status; // note not LPSYSTEM_POWER_STATUS
GetSystemPowerStatus(&status);
if (status.ACLineStatus == 1) errMsg("Online");
// errMsg() is a function I made that makes a quick message box with the phrase in there.
~And indeed, my computer is plugged in.
MSDN makes it seem like you need to link to Kernel32.lib, but my little test runs fine without.
Indeed, the first two lines there will get you all sorts of info about your power. It's up to you to figure out how to use it.
My example only showed me that my computer was plugged in (but most importantly, the code worked). If it wasn't, I would get zero. If something strange happened, I would get 255 (0xFF). To really be safe I should have done:
1 2 3 4 5 6 7 8
SYSTEM_POWER_STATUS status;
if (!GetSystemPowerStatus(&status))
{
// Optionally figure out what happened via GetLastError()
returnfalse;
}
// then use 'status' to figure out what you want to know.