2

I'm creating a hidden window for the purpose of handling messages. I'm experiencing that I do not receive WM_POWERBROADCAST messages in it's GetMessage loop. I do, however, receive it via my WNDPROC. I have confirmed that I do receive other messages in both locations.

Why is GetMessage not receiving WM_POWERBROADCAST?

WNDCLASSEX classInfo = {0};
classInfo.cbSize = sizeof(classInfo);
classInfo.style = WS_DISABLED;
// CustomWndProc just outputs the message and chains to DefaultWndProc
classInfo.lpfnWndProc = CustomWndProc; 
classInfo.hInstance = GetModuleHandle(NULL);
classInfo.hCursor = NULL;
classInfo.hbrBackground = NULL;
classInfo.lpszMenuName = NULL;
classInfo.lpszClassName = L"MyMessageWindow";
ATOM windowClass = RegisterClassEx(&classInfo);

HWND messageWindow = CreateWindowEx(WS_EX_NOACTIVATE, L"MyMessageWindow", 
    L"Message Handling Window", WS_DISABLED, 0, 0, 0, 0, 0, NULL, 
    GetModuleHandle(NULL), NULL);

MSG message;
while (GetMessage(&message, NULL, 0, 0))
{
    // This condition is never true.
    if (message.message == WM_POWERBROADCAST)
        std::cout << "Got WM_POWERBROADCAST" << std::endl;
}
Collin Dauphinee
  • 13,664
  • 1
  • 40
  • 71

1 Answers1

5

That's because WM_POWERBROADCAST is a dispatched synchronously and so is not placed on the message queue.

In order for you to process it you need to handle it in your window procedure.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Wow, it's even at the top of the page, and I somehow didn't notice it. Thanks. – Collin Dauphinee Dec 02 '13 at 22:36
  • I don't understand this solution. So you're saying that I should handle it in window procedure. I did that, I also registered the event and it's still not working. It's ambiguous what to do in continue. Although I get the message, but only once at start. – alap Jun 06 '14 at 11:35
  • 1
    @Laszlo-AndrasZsurzsa I guess you got something wrong along the way. But there's not much more to say than what was in the answer. The question asked why `GetMessage` did not pull the message off into the `message` struct that it returns. And that's because it is a synchronous message. – David Heffernan Jun 06 '14 at 11:46
  • I got what you refer! My problem was something similar, but now I got the idea ... WM_POWERBROADCAST is only good for the software part. So if you want to do the hard way / recognize physical inputs this doesn't help you at all. – alap Jun 06 '14 at 13:00