0

I read in here that WM_POWERBROADCAST messages should be processed inside WindowProc. I tried to capture WM_POWERBROADCAST without a message loop and was not successful. Following is what I tried:

static long FAR PASCAL WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if(message == WM_POWERBROADCAST)
    {
        if (wParam == PBT_APMSUSPEND )

            std::cout << "PBT_APMSUSPEND\n";

        if ( wParam == PBT_APMRESUMESUSPEND )

            std::cout << "PBT_APMRESUMESUSPEND\n";

        return TRUE;
    }
    else
        return DefWindowProc(hWnd, message, wParam, lParam);
}

int main(int argc, char* argv[])
{
    WNDCLASS wc = {0};


    // Set up and register window class
    wc.lpfnWndProc = WindowProc;
    wc.lpszClassName = L"SomeNameYouInvented";
    RegisterClass(&wc);
    HWND hWin = CreateWindow(L"SomeNameYouInvented", L"", 0, 0, 0, 0, 0, NULL, NULL, NULL, 0);
    while(True);
    return 0;
}

Can someone tell me what I am doing wrong or is it even possible to do it without message loop? In case it is not possible without a message loop how is queued and non queued messages different in this aspect.

  • 1
    If you're making a non-interactive app that needs notifications of power change there is [a callback you can register for](https://learn.microsoft.com/en-us/windows/win32/api/powerbase/nf-powerbase-powerregistersuspendresumenotification) – Mgetz Aug 26 '21 at 12:44
  • 1
    for receiver any message, need call `GetMessageW` – RbMm Aug 26 '21 at 12:50
  • Why is important that you do this without a message loop? – Anders Aug 27 '21 at 02:17
  • Just trying different things out, what to know why event loop is needed even if we cannot capture the signal using event loop – Jeeva Kumar Aug 31 '21 at 07:36

0 Answers0