Search code examples
windowsuser32

Send WM_POWERBROADCAST to other process


I have a Windows application that monitors Windows power management events

How to manually send such events to the process for testing purposes rather than putting the laptop to sleep or hibernate? Is there a way to post PBT_APMSUSPEND to a process for example?


Solution

  • You can send the message, with the parameters set up accordingly, using SendMessage, using the recipient applications main window handle as target.

    You might have problems with PBT_POWERSETTINGCHANGE, where lParam is a pointer to a structure. (Windows usually copies such structures to other processes accordingly, but as this is a message you are not supposed to send yourself, it might be handled differently.)

    It would be more prudent anyway to test the real scenario, e.g. in a virtual machine. While you can test your applications response to a particular message, there are aspects this does not cover

    • Some notifications you need to register for (RegisterPowerSettingNotification)
    • Order and timing of notifications
    • Allotted response times
    • behavior of the OS depending on whether your return code indicates you did handle the message or not