Last active
April 15, 2022 18:31
-
-
Save KazWolfe/04e3c7ec52d1b8ea2e939449b1c174dc to your computer and use it in GitHub Desktop.
Quick-n-Dirty MCVE to test getting battery status for Windows computers using Kernel32's GetSystemPowerStatus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <Windows.h> | |
int main() { | |
SYSTEM_POWER_STATUS sps; | |
if (!GetSystemPowerStatus(&sps)) return 1; | |
std::cout << "== BATTERY STATUS ==" << std::endl; | |
std::cout << "Battery Present : " << (sps.BatteryFlag < 100) << std::endl; | |
std::cout << "AC Line Status : " << ((int) sps.ACLineStatus) << std::endl; | |
std::cout << "Battery Flag : " << ((int) sps.BatteryFlag) << std::endl; | |
std::cout << "Battery Charge : " << ((int) sps.BatteryLifePercent) << "%" << std::endl; | |
std::cout << "Battery Life Time : " << ((int) sps.BatteryLifeTime) << " sec" << std::endl; | |
std::cout << "Battery Full Time : " << ((int) sps.BatteryFullLifeTime) << " sec" << std::endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.InteropServices; | |
public class Power { | |
[DllImport("kernel32.dll", SetLastError = true)] | |
private static extern bool GetSystemPowerStatus(out SystemPowerStatus sps); | |
public enum ACLineStatus : byte { | |
Offline = 0, | |
Online = 1, | |
Unknown = 255 | |
} | |
public enum BatteryFlag : byte { | |
High = 1, | |
Low = 2, | |
Critical = 4, | |
Charging = 8, | |
NoSystemBattery = 128, | |
Unknown = 255 | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
public struct SystemPowerStatus { | |
public readonly ACLineStatus LineStatus; | |
public readonly BatteryFlag flgBattery; | |
public readonly byte BatteryLifePercent; | |
public readonly byte Reserved1; | |
public readonly int BatteryLifeTime; | |
public readonly int BatteryFullLifeTime; | |
} | |
public static SystemPowerStatus GetPowerStatus() { | |
GetSystemPowerStatus(out var status); | |
return status; | |
} | |
} | |
public class BatteryMCVE { | |
public static void Main(string[] args) { | |
var sps = Power.GetPowerStatus(); | |
Console.WriteLine("== BATTERY REPORT =="); | |
Console.WriteLine($"Battery Present : {(int) sps.flgBattery < 100}"); | |
Console.WriteLine($"AC Line Status : {Enum.GetName(sps.LineStatus)}"); | |
Console.WriteLine($"Battery Flag : {Enum.GetName(sps.flgBattery)}"); | |
Console.WriteLine($"Battery Charge : {sps.BatteryLifePercent}%"); | |
Console.WriteLine($"Battery Life : {sps.BatteryLifeTime} sec"); | |
Console.WriteLine($"Battery Full Life : {sps.BatteryFullLifeTime} sec"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment