Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program shutdown;
- uses
- Windows;
- const
- WH_MOUSE_LL = 14;
- WH_KEYBOARD_LL = 13;
- WM_KEYDOWN = $0100;
- type
- PKbdDllHookStruct = ^TKbdDllHookStruct;
- _KBDLLHOOKSTRUCT = record
- vkCode: DWORD;
- scanCode: DWORD;
- flags: DWORD;
- time: DWORD;
- dwExtraInfo: PDWORD;
- end;
- TKbdDllHookStruct = _KBDLLHOOKSTRUCT;
- var
- uMsg: tagMSG;
- hMHook: HHOOK;
- hKHook: HHOOK;
- hTimer: UINT;
- dwInterval: UINT;
- bShowing: BOOL = False;
- function MessageBoxTimeout(hWnd: HWND; lpText: PChar; lpCaption: PChar; uType: UINT; wLanguageId: WORD; dwMilliseconds: DWORD): Integer; stdcall; external user32 name 'MessageBoxTimeoutA';
- function ShutdownAccept(): BOOL;
- var
- iResult: Integer;
- begin
- bShowing := True;
- iResult := MessageBoxTimeout(0, 'Компьютер скоро будет выключен. Отменить?', 'Предупреждение', MB_YESNO or MB_ICONWARNING or MB_SETFOREGROUND or MB_SYSTEMMODAL, 0, 60000);
- case iResult of
- IDYES:
- Result := False;
- else
- Result := True;
- end;
- bShowing := False;
- end;
- function SetPrivilege(sPrivilegeName: string; bEnabled: Boolean): Boolean;
- var
- TPPrev, TP: _TOKEN_PRIVILEGES;
- Token: THandle;
- dwRetLen: DWORD;
- begin
- Result := False;
- OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
- TP.PrivilegeCount := 1;
- if LookupPrivilegeValue(nil, PChar(sPrivilegeName), TP.Privileges[0].LUID) then
- begin
- if bEnabled then
- TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
- else
- TP.Privileges[0].Attributes := 0;
- dwRetLen := 0;
- Result := AdjustTokenPrivileges(Token, False, TP, SizeOf(TPPrev), TPPrev, dwRetLen);
- end;
- CloseHandle(Token);
- end;
- function WinExit(iFlags: Integer): Boolean;
- begin
- Result := True;
- if SetPrivilege('SeShutdownPrivilege', True) then
- begin
- if (not ExitWindowsEx(iFlags, 0)) then
- begin
- Result := False;
- end;
- SetPrivilege('SeShutdownPrivilege', False);
- end
- else
- begin
- Result := False;
- end;
- end;
- function ExtractFilePath(const FileName: shortstring): shortstring;
- var
- I: Integer;
- begin
- I := Length(FileName);
- while (I > 1) and not (FileName[I] in ['\', ':']) do
- Dec(I);
- Result := Copy(FileName, 1, I);
- if Result[0] > #0 then
- if Result[Ord(Result[0])] = #0 then
- Dec(Result[0]);
- end;
- procedure TimerProc(hWnd: HWND; uMsg: UINT; nIDEvent: UINT_PTR; ldwTime: DWORD); stdcall;
- begin
- KillTimer(0, hTimer);
- if ShutdownAccept() then
- begin
- UnhookWindowsHookEx(hMHOOK);
- UnhookWindowsHookEx(hKHOOK);
- WinExit(EWX_SHUTDOWN);
- ExitProcess(0);
- end
- else
- begin
- hTimer := SetTimer(0, 0, dwInterval, @TimerProc);
- end;
- end;
- function MouseHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
- begin
- if nCode = HC_ACTION then
- begin
- KillTimer(0, hTimer);
- if not bShowing then
- hTimer := SetTimer(0, 0, dwInterval, @TimerProc);
- end;
- Result := CallNextHookEx(0, nCode, wParam, lParam);
- end;
- function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
- begin
- if (wParam = WM_KEYDOWN) then
- begin
- case PKbdDllHookStruct(lParam)^.vkCode of
- VK_NUMPAD0:
- if Boolean(GetKeyState(VK_RCONTROL)) then
- begin
- UnhookWindowsHookEx(hMHOOK);
- UnhookWindowsHookEx(hKHOOK);
- KillTimer(0, hTimer);
- ExitProcess(0);
- end;
- VK_NUMPAD9:
- if Boolean(GetKeyState(VK_LCONTROL)) then
- begin
- if ShutdownAccept() then
- begin
- UnhookWindowsHookEx(hMHOOK);
- UnhookWindowsHookEx(hKHOOK);
- WinExit(EWX_SHUTDOWN);
- KillTimer(0, hTimer);
- end;
- end;
- else
- begin
- KillTimer(0, hTimer);
- if not bShowing then
- hTimer := SetTimer(0, 0, dwInterval, @TimerProc);
- end;
- end;
- end
- else
- begin
- KillTimer(0, hTimer);
- if not bShowing then
- hTimer := SetTimer(0, 0, dwInterval, @TimerProc);
- end;
- Result := CallNextHookEx(0, nCode, wParam, lParam);
- end;
- begin
- dwInterval := UINT(GetPrivateProfileInt('shutdown', 'interval', 3600000, LPCSTR(ExtractFilePath(ParamStr(0)) + 'config.ini')));
- hTimer := SetTimer(0, 0, dwInterval, @TimerProc);
- hKHook := SetWindowsHookEx(WH_KEYBOARD_LL, @KeyboardHookProc, HInstance, 0);
- hMHOOK := SetWindowsHookEx(WH_MOUSE_LL, @MouseHookProc, hInstance, 0);
- while (GetMessage(uMsg, 0, 0, 0)) do
- begin
- TranslateMessage(uMsg);
- DispatchMessageW(uMsg);
- end;
- UnhookWindowsHookEx(hMHOOK);
- UnhookWindowsHookEx(hKHOOK);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement