Advertisement
HEX0x29A

shutdown

Oct 28th, 2015
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.67 KB | None | 0 0
  1. program shutdown;
  2.  
  3. uses
  4.   Windows;
  5.  
  6. const
  7.   WH_MOUSE_LL = 14;
  8.   WH_KEYBOARD_LL = 13;
  9.   WM_KEYDOWN = $0100;
  10.  
  11. type
  12.   PKbdDllHookStruct = ^TKbdDllHookStruct;
  13.  
  14.   _KBDLLHOOKSTRUCT = record
  15.     vkCode: DWORD;
  16.     scanCode: DWORD;
  17.     flags: DWORD;
  18.     time: DWORD;
  19.     dwExtraInfo: PDWORD;
  20.   end;
  21.  
  22.   TKbdDllHookStruct = _KBDLLHOOKSTRUCT;
  23.  
  24. var
  25.   uMsg: tagMSG;
  26.   hMHook: HHOOK;
  27.   hKHook: HHOOK;
  28.   hTimer: UINT;
  29.   dwInterval: UINT;
  30.   bShowing: BOOL = False;
  31.  
  32. function MessageBoxTimeout(hWnd: HWND; lpText: PChar; lpCaption: PChar; uType: UINT; wLanguageId: WORD; dwMilliseconds: DWORD): Integer; stdcall; external user32 name 'MessageBoxTimeoutA';
  33.  
  34. function ShutdownAccept(): BOOL;
  35. var
  36.   iResult: Integer;
  37. begin
  38.   bShowing := True;
  39.   iResult := MessageBoxTimeout(0, 'Компьютер скоро будет выключен. Отменить?', 'Предупреждение', MB_YESNO or MB_ICONWARNING or MB_SETFOREGROUND or MB_SYSTEMMODAL, 0, 60000);
  40.   case iResult of
  41.     IDYES:
  42.       Result := False;
  43.   else
  44.     Result := True;
  45.   end;
  46.   bShowing := False;
  47. end;
  48.  
  49. function SetPrivilege(sPrivilegeName: string; bEnabled: Boolean): Boolean;
  50. var
  51.   TPPrev, TP: _TOKEN_PRIVILEGES;
  52.   Token: THandle;
  53.   dwRetLen: DWORD;
  54. begin
  55.   Result := False;
  56.   OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
  57.   TP.PrivilegeCount := 1;
  58.   if LookupPrivilegeValue(nil, PChar(sPrivilegeName), TP.Privileges[0].LUID) then
  59.   begin
  60.     if bEnabled then
  61.       TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
  62.     else
  63.       TP.Privileges[0].Attributes := 0;
  64.     dwRetLen := 0;
  65.     Result := AdjustTokenPrivileges(Token, False, TP, SizeOf(TPPrev), TPPrev, dwRetLen);
  66.   end;
  67.   CloseHandle(Token);
  68. end;
  69.  
  70. function WinExit(iFlags: Integer): Boolean;
  71. begin
  72.   Result := True;
  73.   if SetPrivilege('SeShutdownPrivilege', True) then
  74.   begin
  75.     if (not ExitWindowsEx(iFlags, 0)) then
  76.     begin
  77.       Result := False;
  78.     end;
  79.     SetPrivilege('SeShutdownPrivilege', False);
  80.   end
  81.   else
  82.   begin
  83.     Result := False;
  84.   end;
  85. end;
  86.  
  87. function ExtractFilePath(const FileName: shortstring): shortstring;
  88. var
  89.   I: Integer;
  90. begin
  91.   I := Length(FileName);
  92.   while (I > 1) and not (FileName[I] in ['\', ':']) do
  93.     Dec(I);
  94.   Result := Copy(FileName, 1, I);
  95.   if Result[0] > #0 then
  96.     if Result[Ord(Result[0])] = #0 then
  97.       Dec(Result[0]);
  98. end;
  99.  
  100. procedure TimerProc(hWnd: HWND; uMsg: UINT; nIDEvent: UINT_PTR; ldwTime: DWORD); stdcall;
  101. begin
  102.   KillTimer(0, hTimer);
  103.   if ShutdownAccept() then
  104.   begin
  105.     UnhookWindowsHookEx(hMHOOK);
  106.     UnhookWindowsHookEx(hKHOOK);
  107.     WinExit(EWX_SHUTDOWN);
  108.     ExitProcess(0);
  109.   end
  110.   else
  111.   begin
  112.     hTimer := SetTimer(0, 0, dwInterval, @TimerProc);
  113.   end;
  114. end;
  115.  
  116. function MouseHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
  117. begin
  118.   if nCode = HC_ACTION then
  119.   begin
  120.     KillTimer(0, hTimer);
  121.     if not bShowing then
  122.       hTimer := SetTimer(0, 0, dwInterval, @TimerProc);
  123.   end;
  124.   Result := CallNextHookEx(0, nCode, wParam, lParam);
  125. end;
  126.  
  127. function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
  128. begin
  129.   if (wParam = WM_KEYDOWN) then
  130.   begin
  131.     case PKbdDllHookStruct(lParam)^.vkCode of
  132.       VK_NUMPAD0:
  133.         if Boolean(GetKeyState(VK_RCONTROL)) then
  134.         begin
  135.           UnhookWindowsHookEx(hMHOOK);
  136.           UnhookWindowsHookEx(hKHOOK);
  137.           KillTimer(0, hTimer);
  138.           ExitProcess(0);
  139.         end;
  140.       VK_NUMPAD9:
  141.         if Boolean(GetKeyState(VK_LCONTROL)) then
  142.         begin
  143.           if ShutdownAccept() then
  144.           begin
  145.             UnhookWindowsHookEx(hMHOOK);
  146.             UnhookWindowsHookEx(hKHOOK);
  147.             WinExit(EWX_SHUTDOWN);
  148.             KillTimer(0, hTimer);
  149.           end;
  150.         end;
  151.     else
  152.       begin
  153.         KillTimer(0, hTimer);
  154.         if not bShowing then
  155.           hTimer := SetTimer(0, 0, dwInterval, @TimerProc);
  156.       end;
  157.     end;
  158.   end
  159.   else
  160.   begin
  161.     KillTimer(0, hTimer);
  162.     if not bShowing then
  163.       hTimer := SetTimer(0, 0, dwInterval, @TimerProc);
  164.   end;
  165.   Result := CallNextHookEx(0, nCode, wParam, lParam);
  166. end;
  167.  
  168. begin
  169.   dwInterval := UINT(GetPrivateProfileInt('shutdown', 'interval', 3600000, LPCSTR(ExtractFilePath(ParamStr(0)) + 'config.ini')));
  170.   hTimer := SetTimer(0, 0, dwInterval, @TimerProc);
  171.   hKHook := SetWindowsHookEx(WH_KEYBOARD_LL, @KeyboardHookProc, HInstance, 0);
  172.   hMHOOK := SetWindowsHookEx(WH_MOUSE_LL, @MouseHookProc, hInstance, 0);
  173.   while (GetMessage(uMsg, 0, 0, 0)) do
  174.   begin
  175.     TranslateMessage(uMsg);
  176.     DispatchMessageW(uMsg);
  177.   end;
  178.   UnhookWindowsHookEx(hMHOOK);
  179.   UnhookWindowsHookEx(hKHOOK);
  180. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement