【问题标题】:Windows Embedded does not honor ShutdownBlockReasonCreateWindows Embedded 不支持 ShutdownBlockReasonCreate
【发布时间】:2014-06-27 11:00:51
【问题描述】:

以下程序适用于 Windows 7,但不适用于 Windows 7 Embedded Standard Service Pack 1。它不会阻止关机(例如 shutdown /r /t 0)发生。

这是一个与链接为重复的问题类似的问题。但是这个问题的公认答案是“它不起作用”。我还为 Windows 7 中的工作解决方案提供了源代码,该解决方案只需要针对 Windows Embedded 进行一些调整。

任何想法如何让它在嵌入式上工作?或者至少是 Windows 文档指出差异的提示?

#include <windows.h>
#include <stdio.h>

const char g_szClassName[] = "myWindowClass";

// Step 6: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_QUERYENDSESSION:
            OutputDebugStr("Got WM_QUERYENDSESSION message\n");
            char buf[1024];
            sprintf(buf, "Callback %d %d\n", wParam, lParam);
            OutputDebugStr(buf);
            return FALSE; // FALSE should prevent reboot
        break;
        case WM_ENDSESSION:
            OutputDebugStr("Got WM_ENDSESSION message\n");
            Sleep(5000); // Should never get here!
        break;
        case WM_CLOSE:
            OutputDebugStr("Got WM_CLOSE message\n");
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            OutputDebugStr("Got WM_DESTROY message\n");
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: We provide a reason for the shutdown prevention
    BOOL ret = ShutdownBlockReasonCreate(hwnd, L"PreventShutdown running which prevents shutdown");
    if(ret == FALSE)
    {
        MessageBox(NULL, "ShutdownBlockReasonCreate Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 4: We elevate the program to be asked as soon as possible to inhibit shutdown
    ret = SetProcessShutdownParameters(0x4FF, SHUTDOWN_NORETRY)
    if(ret == FALSE)
    {
        MessageBox(NULL, "ShutdownBlockReasonCreate Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    OutputDebugStr("Now starting message loop\n");

    // Step 5: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

【问题讨论】:

    标签: windows embedded shutdown


    【解决方案1】:

    对于这个问题,我们已经有了一个规范的答案,你会发现it here。七百次浏览和半年的研究使这成为最知名的答案,无法做到。

    关注 MSDN 论坛上的 your existing question

    我将用一些图形解释为什么这种功能没有在嵌入式操作系统上实现:

    【讨论】:

    • 这是一个恕我直言(对不起语言)一个愚蠢的答案。在生产嵌入式系统中阻止重启可能没有意义,但它例如可以在开发中有意义。不,WinPe 不是嵌入式系统,它只是微软的半生不熟的尝试,它拥有一些原本会驱使人们使用 Linux 的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    相关资源
    最近更新 更多