Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Some applications need to display battery information to the users all the times. Today WINRT api's don't provide a way for obtaining the battery information. If battery information is absolutely must for you, you could potentially use CallNtPowerInformation (win32) function. Please note that by making a Win32 API call your application will not pass the Windows store certification. This is viable option only if you are side-loading the application.
Which API to Call?
https://msdn.microsoft.com/en-us/library/windows/desktop/aa372675(v=vs.85).aspx particularly the SystemBatteryState information level. This is a Win32 API that can be called from C++ as well as C# code.
How to call the API?
int size = Marshal.SizeOf<Interop.SYSTEM_BATTERY_STATE>();
int status;
IntPtr batteryStatePtr = Marshal.AllocCoTaskMem(size);
try
{
status = Interop.CallNtPowerInformation(
Interop.POWER_INFORMATION_LEVEL.SystemBatteryState,
IntPtr.Zero,
0,
batteryStatePtr,
(uint)size);
if (status >= 0)
{
Interop.SYSTEM_BATTERY_STATE batteryState =
Marshal.PtrToStructure<Interop.SYSTEM_BATTERY_STATE>(batteryStatePtr);
this.Status.Text = String.Format("Battery level: {0}/{1}",
batteryState.RemainingCapacity, batteryState.MaxCapacity);
}
else
{
this.Status.Text = String.Format("Battery level: {0:X}", status);
}
}
finally
{
Marshal.FreeCoTaskMem(batteryStatePtr);
}
static class Interop
{
public enum POWER_INFORMATION_LEVEL {
SystemPowerPolicyAc,
SystemPowerPolicyDc,
VerifySystemPolicyAc,
VerifySystemPolicyDc,
SystemPowerCapabilities,
SystemBatteryState,
SystemPowerStateHandler,
ProcessorStateHandler,
SystemPowerPolicyCurrent,
AdministratorPowerPolicy,
SystemReserveHiberFile,
ProcessorInformation,
SystemPowerInformation,
ProcessorStateHandler2,
LastWakeTime,
LastSleepTime,
SystemExecutionState,
SystemPowerStateNotifyHandler,
ProcessorPowerPolicyAc,
ProcessorPowerPolicyDc,
VerifyProcessorPowerPolicyAc,
VerifyProcessorPowerPolicyDc,
ProcessorPowerPolicyCurrent,
SystemPowerStateLogging,
SystemPowerLoggingEntry,
SetPowerSettingValue,
NotifyUserPowerSetting,
PowerInformationLevelUnused0,
SystemMonitorHiberBootPowerOff,
SystemVideoState,
TraceApplicationPowerMessage,
TraceApplicationPowerMessageEnd,
ProcessorPerfStates,
ProcessorIdleStates,
ProcessorCap,
SystemWakeSource,
SystemHiberFileInformation,
TraceServicePowerMessage,
ProcessorLoad,
PowerShutdownNotification,
MonitorCapabilities,
SessionPowerInit,
SessionDisplayState,
PowerRequestCreate,
PowerRequestAction,
GetPowerRequestList,
ProcessorInformationEx,
NotifyUserModeLegacyPowerEvent,
GroupPark,
ProcessorIdleDomains,
WakeTimerList,
SystemHiberFileSize,
ProcessorIdleStatesHv,
ProcessorPerfStatesHv,
ProcessorPerfCapHv,
ProcessorSetIdle,
LogicalProcessorIdling,
UserPresence,
PowerSettingNotificationName,
GetPowerSettingValue,
IdleResiliency,
SessionRITState,
SessionConnectNotification,
SessionPowerCleanup,
SessionLockState,
SystemHiberbootState,
PlatformInformation,
PdcInvocation,
MonitorInvocation,
FirmwareTableInformationRegistered,
SetShutdownSelectedTime,
SuspendResumeInvocation,
PlmPowerRequestCreate,
ScreenOff,
CsDeviceNotification,
PlatformRole,
LastResumePerformance,
DisplayBurst,
ExitLatencySamplingPercentage,
RegisterSpmPowerSettings,
PlatformIdleStates,
ProcessorIdleVeto,
PlatformIdleVeto,
SystemBatteryStatePrecise,
ThermalEvent,
PowerInformationLevelMaximum
}
public struct SYSTEM_BATTERY_STATE
{
public byte AcOnLine;
public byte BatteryPresent;
public byte Charging;
public byte Discharging;
public byte spare1;
public byte spare2;
public byte spare3;
public byte spare4;
public UInt32 MaxCapacity;
public UInt32 RemainingCapacity;
public Int32 Rate;
public UInt32 EstimatedTime;
public UInt32 DefaultAlert1;
public UInt32 DefaultAlert2;
}
[DllImport("powrprof.dll", SetLastError = true)]
public static extern Int32 CallNtPowerInformation(
POWER_INFORMATION_LEVEL InformationLevel,
IntPtr lpInputBuffer,
UInt32 nInputBufferSize,
IntPtr lpOutputBuffer,
UInt32 nOutputBufferSize);
}