I need to implement a c++ program to capture the power source change event time (AC from/to DC) and do some process when the process occurs. I have seen examples using Event Tracing for Windows (ETW). Is there any other mechanism available to capture windows events?
1 Answers
Create a top-level window to receive WM_POWERBOADCAST
messages, in which you can check for PBT_APMPOWERSTATUSCHANGE
notifications:
Notifies applications of a change in the power status of the computer, such as a switch from battery power to A/C. The system also broadcasts this event when remaining battery power slips below the threshold specified by the user or if the battery power changes by a specified percentage.
Alternatively, you can use RegisterPowerSettingNotification()
to receive WM_POWERBROADCAST
messages containing PBT_POWERSETTINGCHANGE
notifications for GUID_ACDC_POWER_SOURCE
events:
The system power source has changed. The
Data
member is aDWORD
with values from theSYSTEM_POWER_CONDITION
enumeration that indicates the current power source.
Related: How to get the Windows Power State Message (WM_POWERBROADCAST) when not running a Win32 GUI app?

- 555,201
- 31
- 458
- 770
-
Thank you for the answer. How to capture some other events(wake from sleep, modern standby etc) – Arun Dec 14 '22 at 18:47
-
For wake from sleep, read the documentations I linked to. Also see [How can I know when Windows is going into/out of sleep or Hibernate mode?](https://stackoverflow.com/questions/228288/). Unfortunately, AFAIK, there is no (known) way to detect entering/leaving "modern standby" at this time. – Remy Lebeau Dec 14 '22 at 18:54