c++ - 未运行 Win32 GUI 应用程序时如何获取 Windows 电源状态消息 (WM_POWERBROADCAST)?

标签 c++ winapi

所以基本上我有一个由 GUI 应用程序加载的插件 dll。在这个 dll 中我需要检测 Windows 何时进入休眠状态。我无法修改 GUI-App。 GetMessage 仅在调用线程与 UI-Thread 是同一线程时才有效,但事实并非如此。有什么想法吗?

最佳答案

您可以在 DLL 代码的单独线程中创建隐藏窗口。并按如下所示处理消息。

您可以为此使用此 Window 类。

#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);
    }
};

还要确保包含退出条件。

关于c++ - 未运行 Win32 GUI 应用程序时如何获取 Windows 电源状态消息 (WM_POWERBROADCAST)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1165623/

相关文章:

c++ - 如何设置 CvPoint 的值

NASM 程序集中的 Windows API 蜂鸣功能

windows - 图形运行时 DirectShow WAV 文件源不产生任何声音

c++ - Intel Inspector 报告我的自旋锁实现中存在数据竞争

c++ - 如何比较格式为 "Month Date hh:mm:ss"的两个时间戳以检查 +ve 或 -ve 值

c++ - 有没有一种简单的方法来获取用 C++ 打印的字符数?

c++ - 在 Release 模式下访问时 vector 大小为 0

c++ - 何时使用哪种排序算法,何时绝对不应该

delphi - 在工作线程中使用 SHFileOperation 是否安全?

winapi GetSystemTimes函数不填lpIdleTime/lpKernelTime/lpUserTime