1

The following program works on Windows 7 but not on Windows 7 Embedded Standard Service Pack 1. It does not prevent the shutdown (e.g. shutdown /r /t 0) from happening.

This is a similar question as the one which is linked as duplicate. But that question has the accepted answer of "it does not work". I also provide sourcecode to a working solution in Windows 7 which just needs some adjustment for Windows Embedded.

Any ides how to get it working on Embedded? Or at least a hint where the Windows Docu points that difference out?

#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;
}
Fabian
  • 5,476
  • 4
  • 35
  • 46

1 Answers1

0

We already have a canonical answer for this question, you'll find it here. Seven hundred views and half a year of researching this make this the best known answer, it can't be done.

Keep an eye on your existing question on the MSDN Forums.

I'll close with a few graphical explanations why this kind of feature is not implemented on an embedded operating system:

enter image description here enter image description here enter image description here

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • This is a imho (sorry for the language) a stupid answer. It might not make sense to prevent the reboot in a production embedded system but it e.g. can make sense in development. And no, WinPe is not an embedded system its just a half baked try from microsoft to have something which otherwise would drive people to use Linux. – Fabian Sep 29 '15 at 16:09