如何防止系统在没有窗口的情况下关闭?

Sha*_* Li 2 c++ winapi shutdown

我有带有此源代码的Windows应用程序

#include <Windows.h>
#include <thread>
#include <chrono>

int WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR    lpCmdLine,
    int       cmdShow)
{
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}
LRESULT CALLBACK WinProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch (Message)
    {
        case WM_QUERYENDSESSION:
            MessageBox(NULL, "Triggered?", "Message", 0);
            AbortSystemShutdown(NULL);
            return 0;

        default:
            return DefWindowProc(hWnd, Message, wParam, lParam);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我需要知道系统何时关闭并阻止它,或者至少向用户发送一条消息。

我的应用程序似乎没有收到该WM_QUERYENDSESSION消息。

我也尝试过使用,ShutdownBlockReasonCreate()但我没有HWND窗口。

我该怎么做?

zet*_*t42 5

正如参考文献中所述WM_QUERYENDSESSION

窗口通过其 WindowProc 函数接收此消息。

您有一个 WindowProc,但缺少一个窗口。WindowProc 必须与窗口相关联,否则 Windows 无法识别它。要将 WindowProc 与窗口关联起来,您可以调用RegisterClassEx后跟CreateWindowEx. 在调用中指定新创建的窗口类的名称CreateWindowEx

该窗口必须是顶级窗口。它可能是不可见的,但在这种情况下,以下情况适用(来自Windows Vista 中的应用程序关闭更改):

另请注意,如果您的应用程序没有可见的顶级窗口,并且需要成功阻止关闭,则必须使用此 API [ ShutdownBlockReasonCreate() ]。如果此类应用程序在不使用 API 的情况下阻止关闭,则会自动终止。

请注意,仅消息窗口不会接收WM_QUERYENDSESSION.

工作示例:

#include <windows.h>

LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );

int APIENTRY wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPWSTR lpCmdLine, int nCmdShow )
{
    WNDCLASSEXW wx = { sizeof(wx) }; // set cbSize member and zero-initialize all other
    wx.lpfnWndProc = WndProc;
    wx.hInstance = hInstance;
    wx.lpszClassName = L"MyWindowClass";

    if( ! RegisterClassExW( &wx ) )
        return 1;  // TODO: improve error handling

    HWND hWnd = CreateWindowExW( 0, wx.lpszClassName, L"My Application", 0, 0, 0, 0, 0, 
                                 NULL, NULL, NULL, NULL );
    if( ! hWnd )
        return 2;  // TODO: improve error handling

    MSG msg;
    while( GetMessage( &msg, nullptr, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }

    return static_cast<int>( msg.wParam );
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    switch( message )
    {
        case WM_QUERYENDSESSION:
        {
            // Try to block shutdown.
            ShutdownBlockReasonCreate( hWnd, L"I don't want to sleep (yet)!" );
            return FALSE;
        }
        case WM_ENDSESSION:
        {
            // TODO: Always handle this message because shutdown can be forced
            // even if we return FALSE from WM_QUERYENDSESSION!
            return 0;
        }
        default:
        {
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

进一步阅读:

  • @Cheersandhth.-Alf 我现在也已在 Win 7 和 Win 8.1 下成功测试了上述代码。[此页面](https://msdn.microsoft.com/en-us/library/windows/desktop/bb394721(v=vs.85).aspx) 上也证实了此行为:“_但是,如果应用程序没有可见的顶级窗口使用新的 API 主动指示它需要阻止关闭,Windows Vista 不会自动终止它,而是将其视为具有可见顶级窗口的应用程序。_”。在页面的更下方,“新 API”被定义为“ShutdownBlockReasonCreate()”。 (2认同)