How to handle system power change notifications?

27 07 2008

Today i was watching one movie a in HP QuickPlay application. In the mean while, the playback suddenly stopped and it showed a messagebox that the battery is running out.

At that moment i noticed that i didn’t connected the power cord. But how did the application identified the battery is critical?

When the power status changes the system broadcast the message – WM_POWERBROADCAST. Just handle the message and you’ll get notified about power related events. Have a look at the code snippet.

For handling the message, modify the message map as follows.


Now add function – LRESULT OnPowerBroadcast( WPARAM wParam, LPARAM lParam ) to your dialog class with body as follows.

// Handle Power Event Notifications.
LRESULT CDialogDlg::OnPowerBroadcast( WPARAM wParam,
                                      LPARAM lParam )
    // Check whether the battery is low.
    if( PBT_APMBATTERYLOW == wParam )
        // Yes. So shut down the operations.
    return TRUE;

There are a lot more notification other than PBT_APMBATTERYLOW that you can check. They are –

  • PBT_APMBATTERYLOW – Battery power is low.
  • PBT_APMOEMEVENT – OEM-defined event occurred.
  • PBT_APMPOWERSTATUSCHANGE – Power status has changed.
  • PBT_APMQUERYSUSPEND – Request for permission to suspend.
  • PBT_APMQUERYSUSPENDFAILED – Suspension request denied.
  • PBT_APMRESUMEAUTOMATIC – Operation resuming automatically after event.
  • PBT_APMRESUMECRITICAL – Operation resuming after critical suspension.
  • PBT_APMRESUMESUSPEND – Operation resuming after suspension.
  • PBT_APMSUSPEND – System is suspending operation.

Well, don’t ask me – “Which movie you was keenly watching?” 😉

Targeted Audience – Beginners.