首页  编辑  

一个禁止Sys Key的Hook DLL

Tags: /超级猛料/Hook.钩子/   Date Created:

{ 不过下面的不能禁止Ctrl+Alt+Del,其他的好像都可以 }

library Keys;

{

# ***************************************************************************

#     Keys DLL             -   DLL for disabling system keys

#                              -----------------------------

#     date                 : Tue Apr 29 2004

#     copyright            : (C) 2003 by Paschalis Pagonidis

#     email                : ppag@internet.gr

#     homepage             : http://www.netsoft.gr/pascal/

# ***************************************************************************

# ***************************************************************************

# *                                                                         *

# *   This program is free software; you can redistribute it and/or modify  *

# *   it under the terms of the GNU General Public License as published by  *

# *   the Free Software Foundation; either version 2 of the License, or     *

# *   (at your option) any later version.                                   *

# *                                                                         *

# ***************************************************************************

}

uses

       Windows, Messages, SysUtils;

{$R *.res}

{Define a record for recording and passing information process wide}  

type

       PHookRec = ^THookRec;

       THookRec = packed record

               TheHookHandle : HHOOK;

               TheAppWinHandle : HWND;

               TheCtrlWinHandle : HWND;

               TheKeyCount : DWORD;

       end;

const

       WH_KEYBOARD_LL = 13;

       LLKHF_ALTDOWN  = KF_ALTDOWN shr 8;

var

       hObjHandle : THandle; {Variable for the file mapping object}

       lpHookRec : PHookRec; {Pointer to our hook record}

//        HookHandle : HHOOK;

procedure MapFileMemory(dwAllocSize : DWORD);

begin

       {Create a process wide memory mapped variable}

       hObjHandle := CreateFileMapping($FFFFFFFF, NIL, PAGE_READWRITE, 0, dwAllocSize, 'HookRecMemBlock');

       if (hObjHandle = 0) then

       begin

               MessageBox(0, 'Keys DLL', 'Could not create file map object', MB_OK);

               exit;

       end;

       {Get a pointer to our process wide memory mapped variable}

       lpHookRec := MapViewOfFile(hObjHandle, FILE_MAP_WRITE, 0, 0, dwAllocSize);

       if (lpHookRec = NIL) then

       begin

               CloseHandle(hObjHandle);

               MessageBox(0, 'Keys DLL', 'Could not map file', MB_OK);

               exit;

       end;

end;

procedure UnMapFileMemory;

begin

       {Delete our process wide memory mapped variable}

       if (lpHookRec <> NIL) then

       begin

               UnMapViewOfFile(lpHookRec);

               lpHookRec := NIL;

       end;

       if (hObjHandle > 0) then

       begin

               CloseHandle(hObjHandle);

               hObjHandle := 0;

       end;

end;

function GetHookRecPointer : pointer stdcall;

begin

       {Return a pointer to our process wide memory mapped variable}

       Result := lpHookRec;

end;

// hook code, virtual-key code, keystroke-message information

function HookSysKeys(nCode: Integer; wParam: WPARAM; lParam: LPARAM): Integer; stdcall;

type

       // from Platfrom SDK

       PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;

       TKBDLLHOOKSTRUCT = record

               vkCode,

               scanCode,

               flags,

               time:        DWORD;

               dwExtraInfo: Pointer;

       end;

var

       kbdll: PKBDLLHOOKSTRUCT;

begin

       Result := 0;

       case nCode of

               HC_ACTION:

               begin

                       {We trap the keystrokes here}

                       kbdll := Pointer(lParam);

                       

                       {Disable CTRL+ESC}

                       if (kbdll.vkCode = VK_ESCAPE) and

                                       ((GetAsyncKeyState (VK_CONTROL) and (1 shl 15)) <> 0) then

                               Result := 1

                       {Disable ALT+TAB, ALT+ESC}

                       else if (kbdll.vkCode in [VK_TAB, VK_ESCAPE]) and

                                       ((GetAsyncKeyState (VK_MENU) and (1 shl 15)) <> 0) then

                               Result := 1

                       {Disable WinKey}

                       else if (kbdll.vkCode = VK_LWIN) or (kbdll.vkCode = VK_RWIN) then

                               Result := 1;

               end;

       end;

       if Result <> 1 then

               Result := CallNextHookEx(0, nCode, wParam, lParam);

end;

procedure EnableSysKeys; stdcall;

begin

       {If we have a process wide memory variable}

       {and the hook has not already been set...}

       if ((lpHookRec <> NIL) AND (lpHookRec^.TheHookHandle = 0)) then

       begin

               {Set the hook and remember our hook handle}

               lpHookRec^.TheHookHandle :=

                       SetWindowsHookEx(WH_KEYBOARD_LL, @HookSysKeys, hInstance, 0);

       end;

end;

procedure DisableSysKeys; stdcall;

begin

       {If we have a process wide memory variable}

       {and the hook has already been set...}

       if ((lpHookRec <> NIL) AND (lpHookRec^.TheHookHandle <> 0)) then

       begin

               {Remove our hook and clear our hook handle}

               if (UnHookWindowsHookEx(lpHookRec^.TheHookHandle) <> FALSE) then

//                UnHookWindowsHookEx(HookHandle);

                       lpHookRec^.TheHookHandle := 0;

       end;

end;

procedure DllEntryPoint(dwReason : DWORD);

begin

       case dwReason of

               Dll_Process_Attach:

               begin

                       {If we are getting mapped into a process, then get}

                       {a pointer to our process wide memory mapped variable}

                       hObjHandle := 0;

                       lpHookRec := NIL;

                       MapFileMemory(sizeof(lpHookRec^));

               end;

               Dll_Process_Detach:

               begin

                       {If we are getting unmapped from a process then, remove}

                       {the pointer to our process wide memory mapped variable}

                       UnMapFileMemory;

               end;

       end;

end;

exports

       HookSysKeys,

       GetHookRecPointer,

       EnableSysKeys,

       DisableSysKeys;

begin

       {Set our Dll's main entry point}

       DLLProc := @DllEntryPoint;

       {Call our Dll's main entry point}

       DllEntryPoint(Dll_Process_Attach);

end.