获取有关 Windows 将在 C++ 中休眠/唤醒的通知 [英] Get Notified about Windows is Going to Sleep/Waking up in C++

查看:104
本文介绍了获取有关 Windows 将在 C++ 中休眠/唤醒的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它有多个线程等待来自 DLL 和串行端口的不同输入.

I am working on an App which has Multiple Threads waiting for different inputs from DLLs and Serial Ports.

我想添加一个功能,在机器进入睡眠状态之前,我必须卸载某些 DLL,并且在醒来时必须重新加载 DLL.为此,我需要在睡眠和唤醒时收到通知.

I want to add a functionality that before machine going to Sleep, I have to unload certain DLL and On waking up have to Re-load the DLL. For this, I need to get notified on Sleep and Wake up.

我发现很多关于用 C# 做的文件,但我想用 C++ 做这个.

I found many files about doing in C# but I want to do this in C++.

我尝试使用此代码项目,但无法捕获任何事件.我删除了与 Window Paint 和所有相关的所有内容,因为我不需要它的 GUI 并且只保留 主消息循环(主循环中的 While 循环)

I tried using this code Project but could not capture any event. I removed everything related to Window Paint and All that as I do not need it's GUI and kept only main message loop (The While loop in the main)

-

我使用它作为我的主循环:-

I am using this as my main loop:-

 // Start the message loop. 

while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
} 

坦率地说,我从 CodeProject 复制了这个,并且只做了一个修改,即从 MSDN 文章中检查了 GetMessage(..) != 0.

To be frank I have copied this from CodeProject, and Made only one Modification i.e. Checked GetMessage(..) != 0 from a MSDN Article.

我错过了什么吗?

或者任何其他解决方案??

Or anyother Solution??

我正在使用 VS2010 和 C++ 编程

I am using VS2010 and programming in C++

提前致谢!

推荐答案

尝试处理 WM_POWERBROADCAST 消息

这是应该可以工作的示例代码.显然,您确实需要创建一个窗口,否则您将收不到消息.该示例创建了一个隐藏窗口来实现此目的.

Here's sample code that should work. Apparently you do need to create a window otherwise you don't receive the messages. The sample creates a hidden window to achieve this.

static long FAR PASCAL WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if(message == WM_POWERBROADCAST)
    {
        //Do something
        return TRUE;
    }
    else
        return DefWindowProc(hWnd, message, wParam, lParam);
}

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


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

    BOOL bRet;
    MSG msg;
    while( (bRet = GetMessage( &msg, hWin, 0, 0 )) != 0)
    { 
        if (bRet == -1)
        {
            // handle the error and possibly exit
        }
        else
        {
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        }
    }
    return 0;
}

这篇关于获取有关 Windows 将在 C++ 中休眠/唤醒的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆