#include #include "hooklib\hooklib.h" /* local prototypes */ DWORD AlertThreadProc(LPVOID); LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM); /* application entry-point */ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow ) { WNDCLASSEX wc; HWND hwnd; MSG msg; int ret; /* register our window class */ RtlZeroMemory(&wc, sizeof(WNDCLASSEX)); wc.cbSize = sizeof(WNDCLASSEX); wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = L"LifeGuardWindow"; RegisterClassEx(&wc); /* create the message window */ hwnd = CreateWindowEx(0, L"LifeGuardWindow", NULL, 0, 0, 0, 0, 0, 0, 0, hInstance, 0); if( !IsWindow(hwnd) ) { MessageBox(0, L"Failed to create message window.", L"LifeGuard", MB_ICONERROR); return GetLastError(); } /* notify this process first about a pending shut down */ SetProcessShutdownParameters(0x4FF, 0); /* register the message hook */ ret = RegisterHook(hwnd); if( ret != ERROR_SUCCESS ) { ret = GetLastError(); DestroyWindow(hwnd); MessageBox(0, L"Failed to register message hook.", L"LifeGuard", MB_ICONERROR); return ret; } /* message pump */ while( GetMessage(&msg, hwnd, 0, 0) > 0 ) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } /* thread procedure for shut down alerts */ DWORD AlertThreadProc( LPVOID lpv ) { MessageBox(0, L"A system shut down request was detected.", L"LifeGuard", MB_ICONWARNING); return 0; } /* window message procedure */ LRESULT CALLBACK WindowProc( HWND hWnd, UINT uiMessage, WPARAM wParam, LPARAM lParam ) { if( uiMessage == WM_USER ) { /* alert the user that a shut down message was blocked */ CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)AlertThreadProc, NULL, STANDARD_RIGHTS_REQUIRED, NULL); return 0; } else return DefWindowProc(hWnd, uiMessage, wParam, lParam); }