How to receive the Windows Power State Message (WM_POWERBROADCAST) when the Win32 GUI application does not start?

So basically I have a dll plugin loaded by a GUI. In this dll, I need to determine when Windows goes into Hibernate state. I can not change the GUI application. GetMessage only works if the calling thread is the same thread as the UI-Thread, which is not. Any ideas?

+3
source share
3 answers

You can create a hidden window in a separate thread from your DLL code. And process the messages as shown below.

You can use this Window class to do this.

#pragma once

#include <windows.h>
#include <process.h>
#include <iostream>

using namespace std;

static const char *g_AppName  = "Test";

class CMyWindow
{
    HWND  _hWnd;
    int _width;
    int _height;
public:
    CMyWindow(const int width,const int height):_hWnd(NULL),_width(width),_height(height)
    {
        _beginthread( &CMyWindow::thread_entry, 0, this);
    }

    ~CMyWindow(void)
    {
        SendMessage(_hWnd, WM_CLOSE, NULL, NULL);
    }


private:
    static void thread_entry(void * p_userdata)
    {
        CMyWindow * p_win = static_cast<CMyWindow*> (p_userdata);
        p_win->create_window();
        p_win->message_loop();
    }

    void create_window()
    {
        WNDCLASSEX wcex;

        wcex.cbSize         = sizeof(WNDCLASSEX);
        wcex.style          = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = &CMyWindow::WindowProc;
        wcex.cbClsExtra     = 0;
        wcex.cbWndExtra     = 0;
        wcex.hInstance      = GetModuleHandle(NULL);
        wcex.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName   = NULL;
        wcex.lpszClassName  = g_AppName;
        wcex.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);

        RegisterClassEx(&wcex);

        _hWnd = CreateWindow(g_AppName, g_AppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, GetModuleHandle(NULL), NULL);

        ShowWindow(_hWnd, SW_SHOWDEFAULT);
        UpdateWindow(_hWnd);
    }

    void message_loop()
    {
        MSG msg = {0};

        while (GetMessage(&msg, NULL, 0, 0))
        {
            if(msg.message == WM_QUIT)
            {
                break;
            }

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    static LRESULT WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch(uMsg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        case WM_POWERBROADCAST:
            {
                //power management code here
            }

        }

        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
};

Also be sure to include the exit condition.

+8
source

DLL HWND. , , GetWindowLongPtr proc (GWL_WNDPROC), SetWindowLongPtr proc , WM_POWERBROADCAST , GetWindowLongPtr.

DLL , SetWindowLongPtr proc , DLL , highigs .

+1

Windows. , , . , , , "".

, DLL. , , ( ), WM_POWERBROADCAST, , Windows, .

It is worth noting, by the way, that you are not guaranteed to receive a notification before the system goes into sleep mode (for example, from a critical battery state) or into any other standby state. However, you will receive PBT_APMRESUMEAUTOMATIC (or PBT_APMRESUMECRITICAL on systems prior to Vista) when the system returns to the network after such an event.

+1
source

Source: https://habr.com/ru/post/1713238/


All Articles