以下程序适用于Windows 7,但不适用于Windows 7 Embedded Standard Service Pack 1.它不会阻止关机(例如shutdown / r / t 0)的发生。
这是一个与重复链接的问题类似的问题。但是这个问题已经接受了#34;它不起作用"。我还为Windows 7中的工作解决方案提供了源代码,只需要对Windows Embedded进行一些调整。
任何想法如何让它在Embedded上运行?或者至少提示Windows Docu指出这种差异的提示?
#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;
}
答案 0 :(得分:0)
我们已经有了这个问题的规范答案,您会找到it here。七百个观点和半年的研究使这成为最着名的答案,它无法完成。
密切关注MSDN论坛上的your existing question。
我将结束一些图解说明为什么在嵌入式操作系统上没有实现此类功能: