unithookdll.~pas

来自「屏幕取词的小例子 是Delphi下深入Windows核心编程 的一个经典例子」· ~PAS 代码 · 共 96 行

~PAS
96
字号
unit UnitHookDLL;
interface
uses Windows, Messages, Dialogs, SysUtils;

  function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; export;
  function CloseGetKeyHook: BOOL; export;

function LoadLibrary16(LibraryName: PChar): THandle; stdcall; external kernel32 index 35;
procedure FreeLibrary16(HInstance: THandle); stdcall; external kernel32 index 36;
function GetProcAddress16(Hinstance: THandle; ProcName: PChar): Pointer; stdcall; external kernel32 index 37;
procedure QT_Thunk; cdecl; external kernel32 name 'QT_Thunk';
var
  hInst16: THandle; {}
  pFuncCreate,pFuncFree: Pointer; {函数指针}
  MessageHook: THandle;
{ QT_Thunk 需要堆栈}
{$StackFrames On}


implementation

function SetGDIHook: boolean;
begin
  result:=false;
  if pFuncCreate = nil then exit;
  asm
     {保存寄存器的值}
      pushad
      push ebp
      sub esp,$2c
      mov edx, pFuncCreate{函数地址}
      mov ebp,esp
      add ebp,$2c
      call  QT_Thunk
      add esp,$2c
      pop ebp
      mov byte ptr result,al

      popad
  end;
end;

function UnSetGDIHook: boolean;
var
   reserve:array[0..$40] of char; //堆栈,一定要在最下!!!
begin
  result:=false;
  if pFuncCreate = nil then exit;
  asm
    mov edx, offset reserve //保留,无作用,但不能删除!!!
    mov edx, pFuncFree     { 载入16-bit过程指针 }
    call QT_Thunk     {调用替换程序 }
    mov byte ptr result, al    { 保存结果}
  end;
end;

function GetMsgProc(code: integer; removal: integer; msg: integer): Integer; stdcall;
begin
  Result:=0;
end;

function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; export;
begin
   MessageHook:=SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, HInstance, 0);
   SetGDIHook;
   result:=true;
end;

function CloseGetKeyHook: BOOL; export;
begin
   UnSetGDIHook;
   UnhookWindowsHookEx(MessageHook);   
   result:=true;   
end;

initialization
    pFuncFree:=nil;
    pFuncCreate:=nil;
    hInst16 := LoadLibrary16('project1.DLL');
    if hInst16 >= 32 then
    begin
       pFuncCreate := GetProcAddress16(hInst16, 'TextHookCreate');
       pFuncFree := GetProcAddress16(hInst16, 'TextHookFree');
       if (pFuncCreate=nil)or(pFuncFree=nil) then
       begin
          pFuncCreate:=nil;
          pFuncFree:=nil;
       end;
    end;
finalization
    FreeLibrary16(hInst16);

end.


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?