library hookdemo;<br><br>uses<br> SysUtils,<br> Classes,windows,messages;<br><br>{$R *.RES}<br>const<br> CopyMessage=wm_user+1001;<br> HookMemFileName='Hook.DAT';<br><br><br>type<br> THookrec=record<br> hookerhandle:THandle;//your app<br> hookedhandle:THandle;//be hooked app<br> copyhook:integer;<br> end;<br> PHookRec=^THOOKrec;<br>var<br> hookrec
HOOKRec;<br> memfile:THandle;<br>{------------ Hook procedures ------------------}<br>// Callback of the Keyboard Hook<br>function CopyHookProc(Code:integer;// hook code<br> wParam:WPARAM;// removal flag 没用<br> lParam:lParam // address of structure with message<br> ):LRESULT;stdcall;<br>var<br> Buffer
Char;<br> buffersize:integer;<br> copyStruct:Tcopydatastruct;<br>begin<br> if code=HC_ACTION then<br> begin<br> if (pmsg(lParam)^.hwnd=hookrec^.hookedhandle) and (PMsg(lParam)^.message=wm_settext) then<br> begin<br> Buffersize:=strlen(PChar(pmsg(lParam)^.lParam))+1*SizeOf(char);<br> getmem(Buffer,buffersize);<br> strcopy(Buffer,PChar(pmsg(lParam)^.lParam));<br> with copystruct do<br> begin<br> dwdata:=wm_user+1001;<br> cbdata:=buffersize;<br> lpdata:=Buffer;<br> end;<br> SendMessage(Hookrec^.hookerhandle,wm_copydata,0,Longint(@copystruct));<br> freemem(Buffer,buffersize);<br> end<br> end;<br> //HC_NoREmove need not process<br> Result := CallNextHookEx(Hookrec^.copyHook, Code, wParam, lParam);<br>end;<br><br><br>{----------------Procedures called by TCXHook component----------------}<br>procedure InstallHook; stdcall;<br>begin<br> Hookrec^.copyHook:=SetWindowsHookEx(WH_GetMessage, @copyHookProc, HInstance , 0);<br>end;<br>//UnInstallHook<br>procedure UnHook; stdcall;<br>begin<br> UnhookWindowsHookEx(Hookrec^.copyHook);<br>end;<br><br>procedure Sethandle(a,b:Integer); stdcall;<br>begin<br> hookrec^.hookerhandle:=a;<br> Hookrec^.hookedhandle:=b;<br>end;<br><br>procedure IntoDll; stdcall;<br>begin<br> MemFile:=OpenFileMapping(FILE_MAP_WRITE,False,HookMemFileName);<br> if MemFile=0 then<br> MemFile:=CreateFileMapping($FFFFFFFF,nil,<br> PAGE_READWRITE,0,SizeOf(THookRec),HookMemFileName);<br> HookRec:=MapViewOfFile(MemFile,FILE_MAP_WRITE,0,0,0);<br> if MemFile=0 then<br> FillChar(HookRec^,SizeOf(THookRec),0);<br>end;<br><br>procedure ExitDll; stdcall;<br>begin<br> UnmapViewOfFile(HookRec);<br> CloseHandle(MemFile);<br>end;<br><br>procedure DLLEntryPoint(reason:integer);<br>begin<br> case reason of<br> 0: {DLL_PROCESS_DETACH} ExitDll;<br> 1: {DLL_PROCESS_ATTACH} IntoDll;<br> end;<br>end;<br><br><br>exports<br> InstallHook,<br> UnHook,<br> Sethandle ;<br><br>begin<br> Intodll;<br> DLLProc:=@DLLEntryPoint;<br>end.<br>//上面是钩子的dll<br>//下面是主程序<br>unit ccopyhook;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Memo1: TMemo;<br> Button1: TButton;<br> procedure wmcopy(var message:TMessage);message wm_copydata;<br> procedure Button1Click(Sender: TObject);<br> procedure FormDestroy(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br>procedure InstallHook; external 'Hookdemo.dll' name 'InstallHook';<br>procedure unhook;external 'hookdemo.dll' Name 'unhook';<br>procedure sethandle;external 'hookdemo.dll' Name 'sethandle';<br><br>procedure TForm1.wmcopy(var message: TMessage);<br>begin<br> if pcopydatastruct(message.lParam)^.dwdata=wm_user+1001 then<br> memo1.SetTextBuf(PChar(pcopydatastruct(message.lParam)^.lpData));<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> a,b:integer;<br>begin<br> b:=findwindow(yourwanttohook);<br> a:=handle;<br> sethandle(a,b);<br> installhook;<br>end;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br> unhook;<br>end;<br><br>end.<br>//你试一下吧,我没试可能有bug.