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

How to prevent a system shutdown without having a window?

我有 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;
}

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

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

我也尝试过使用 ShutdownBlockReasonCreate() 但我没有 HWND 用于 window。

我应该怎么做?

WM_QUERYENDSESSION 的参考文献所述:

A window receives this message through its WindowProc function.

您有一个 WindowProc,但缺少 window。 WindowProc 必须与 window 相关联,否则 Windows 不知道它。要将 WindowProc 与 window 相关联,您可以调用 RegisterClassEx,然后调用 CreateWindowEx。在对 CreateWindowEx.

的调用中指定新创建的 window class 的名称

window 必须是顶级 window。它可以是不可见的,但在这种情况下适用以下内容(来自 Application Shutdown Changes in Windows Vista):

Also note that if your application has no visible top-level windows, it must use this API [ShutdownBlockReasonCreate()] if it needs to successfully block shutdown. Such applications will automatically be terminated if they block shutdown without using the API.

请注意 message-only window will not receive 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;
}

进一步阅读: