大部分类型的钩子都在这儿了。<br><br><br>----------这是*.dll中的单元---------------<br>unit HookProc;<br><br><br>interface<br><br>uses windows,messages,sysutils;<br><br>const<br> HTName:array[1..13] of pchar=(<br> 'CALLWNDPROC','CALLWNDPROCRET','CBT','DEBUG','GETMESSAGE','JOURNALPLAYBACK',<br> 'JOURNALRECORD','KEYBOARD','MOUSE','MSGFILTER','SHELL','SYSMSGFILTER','FOREGROUNDIDLE'<br> );<br><br><br>function CallWndProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function CallWndRetProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function CBTProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function DebugProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function GetMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function JournalPlaybackProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function JournalRecordProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function KeyboardProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function MouseProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function MessageProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function ShellProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function SysMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>function ForegroundIdleProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br><br>implementation<br><br>procedure SaveInfo(k:integer;str:string);stdcall;<br>var<br> f:textfile;<br> WorkPath:string;<br>begin<br> WorkPath:=ExtractFilePath(ParamStr(0));<br> assignfile(f,WorkPath+'Records.txt');<br> if fileexists(WorkPath+'Records.txt')=false then rewrite(f)<br> else append(f);<br> //if strcomp(pchar(str),pchar('#13#10'))=0 then writeln(f,'')<br> //else write(f,str);<br> writeln(f,HTName[k]+'----'+str);<br> closefile(f);<br>end;<br><br><br><br>function CallWndProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>var<br> pcs:TCWPSTRUCT;<br>begin<br> pcs:=TCWPSTRUCT(PCWPSTRUCT(lParam)^);<br> if nCode>=0 then<br> begin<br> if pcs.message=wm_lbuttonup then<br> SaveInfo(1,format('hwnd=%x',[pcs.hwnd]));<br> end;<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function CallWndRetProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function CBTProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function DebugProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function GetMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>var<br> pcs:TMSG;<br>begin<br> pcs:=TMSG(PMSG(lParam)^);<br> if nCode>=0 then<br> begin<br> if pcs.message=wm_lbuttonup then<br> SaveInfo(5,format('hwnd=%x',[pcs.hwnd]));<br> end;<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function JournalPlaybackProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function JournalRecordProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function KeyboardProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function MouseProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function MessageProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function ShellProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function SysMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br>//<br>function ForegroundIdleProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=CallNextHookEx(0,nCode,wParam,lParam);<br>end;<br><br><br>end.<br><br><br><br>--------这是*.dll主程序------------------<br>library DemoHook;<br><br>uses<br> windows,messages,sysutils,<br> HookProc in 'HookProc.pas';<br><br>{$r *.res}<br><br>const<br><br> HookMemFileName='DllHookMemFile.DTA';<br> HTName:array[1..13] of pchar=(<br> 'CALLWNDPROC','CALLWNDPROCRET','CBT','DEBUG','GETMESSAGE','JOURNALPLAYBACK',<br> 'JOURNALRECORD','KEYBOARD','MOUSE','MSGFILTER','SHELL','SYSMSGFILTER','FOREGROUNDIDLE'<br> );<br><br>type<br> THookProc = function(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br> PShared=^TShared;<br> THook = record<br> HookHand:HHook;<br> HookType:integer;<br> HookProc:THookProc;<br> end;<br> TShared = record<br> Hook:array [0..16] of THook;<br> Father,Self:integer;<br> Count:integer;<br> hinst:integer;<br> end;<br> TWin = record<br> Msg:TMsg;<br> wClass:TWndClass;<br> hMain:integer;<br> end;<br>var<br> MemFile:THandle;<br> Shared
Shared;<br> Win:TWin;<br> wmhook:integer;<br><br>procedure SaveInfo(k:integer;str:string);stdcall;<br>var<br> f:textfile;<br> WorkPath:string;<br>begin<br> WorkPath:=ExtractFilePath(ParamStr(0));<br> assignfile(f,WorkPath+'Records.txt');<br> if fileexists(WorkPath+'Records.txt')=false then rewrite(f)<br> else append(f);<br> //if strcomp(pchar(str),pchar('#13#10'))=0 then writeln(f,'')<br> //else write(f,str);<br> writeln(f,HTName[k]+'----'+str);<br> closefile(f);<br>end;<br><br><br>procedure InitHookData;<br>var k:integer;<br>begin<br> with Shared^ do<br> begin<br> for k:=0 to 14 do Hook[k].HookHand:=0;<br> //<br> Hook[0].HookType:=WH_CALLWNDPROC;<br> Hook[0].HookProc:=@CallWndProc;<br> //<br> Hook[1].HookType:=WH_CALLWNDPROCRET;<br> Hook[1].HookProc:=@CallWndRetProc;<br> //<br> Hook[2].HookType:=WH_CBT;<br> Hook[2].HookProc:=@CBTProc;<br> //<br> Hook[3].HookType:=WH_DEBUG;<br> Hook[3].HookProc:=@DebugProc;<br> //<br> Hook[4].HookType:=WH_GETMESSAGE;<br> Hook[4].HookProc:=@GetMsgProc;<br> //<br> Hook[5].HookType:=WH_JOURNALPLAYBACK;<br> Hook[5].HookProc:=@JournalPlaybackProc;<br> //<br> Hook[6].HookType:=WH_JOURNALRECORD;<br> Hook[6].HookProc:=@JournalRecordProc;<br> //<br> Hook[7].HookType:=WH_KEYBOARD;<br> Hook[7].HookProc:=@KeyboardProc;<br> //<br> Hook[8].HookType:=WH_MOUSE;<br> Hook[8].HookProc:=@MouseProc;<br> //<br> Hook[9].HookType:=WH_MSGFILTER;<br> Hook[9].HookProc:=@MessageProc;<br> //<br> Hook[10].HookType:=WH_SHELL ;<br> Hook[10].HookProc:=@ShellProc;<br> //<br> Hook[11].HookType:=WH_SYSMSGFILTER;<br> Hook[11].HookProc:=@SysMsgProc;<br> //<br> Hook[12].HookType:=WH_FOREGROUNDIDLE;<br> Hook[12].HookProc:=@ForegroundIdleProc;<br><br> end;<br>end;<br><br>function SetHook(fSet:boolean;HookId:integer):bool;stdcall;<br>begin<br> with shared^ do<br> if fSet=true then<br> begin<br> if Hook[HookId].HookHand=0 then<br> begin<br> Hook[HookId].HookHand:=SetWindowsHookEx(Hook[HookId].HookType,Hook[HookId].HookProc,hinstance,0);<br> if Hook[HookId].HookHand<>0 then Result:=true<br> else Result:=false;<br> end else Result:=true;<br> end else<br> begin<br> if Hook[HookId].HookHand<>0 then<br> begin<br> if UnhookWindowsHookEx(Hook[HookId].HookHand)=true then<br> begin<br> Hook[HookId].HookHand:=0;<br> Result:=true;<br> end else Result:=false;<br> end else Result:=true;<br> end;<br>end;<br><br>procedure Extro;<br>begin<br> UnmapViewOfFile(Shared);<br> CloseHandle(MemFile);<br>end;<br><br><br>function WindowProc(hWnd,Msg,wParam,lParam:longint):LRESULT; stdcall;<br>var k:integer;<br>begin<br> Result:=DefWindowProc(hWnd,Msg,wParam,lParam);<br> case Msg of<br> wm_destroy:<br> begin<br> for k:=0 to 12 do SetHook(False,k);<br> postmessage(findwindow('WinHook',nil),wm_destroy,0,0);<br> ExitThread(0);<br> end;<br> end;<br> if msg=wmhook then<br> begin<br> if wparam>0 then<br> begin<br> if sethook(true,wparam-1)=true then postmessage(findwindow('WinHook',nil),wmhook,wparam,0);<br> end else<br> begin<br> if sethook(false,-wparam-1)=true then postmessage(findwindow('WinHook',nil),wmhook,wparam,0);<br> end;<br> end;<br>end;<br><br>procedure run;stdcall;<br>//var k:integer;<br>begin<br> win.wClass.lpfnWndProc:= @WindowProc;<br> win.wClass.hInstance:= hInstance;<br> win.wClass.lpszClassName:='WideHook';<br> RegisterClass(win.wClass);<br> win.hmain:=CreateWindowEx(ws_ex_toolwindow,win.wClass.lpszClassName,'WideHook',WS_CAPTION,0,0,1,1,0,0,hInstance,nil);<br> FillChar(Shared^,SizeOf(TShared),0);<br> shared^.self:=win.hmain;<br> shared^.hinst:=hinstance;<br> InitHookData;<br> wmhook:=registerwindowmessage(pchar('wm_hook'));<br> while(GetMessage(win.Msg,win.hmain,0,0))do<br> begin<br> TranslateMessage(win.Msg);<br> DispatchMessage(win.Msg);<br> end;<br>end;<br><br>procedure DllEntryPoint(fdwReason
WORD);<br>begin<br> case fdwReason of<br> DLL_PROCESS_DETACH:<br> Extro;<br> end;<br>end;<br><br>exports run;<br><br>begin<br> //建立内存映象文件,用来保存全局变量<br> MemFile:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShared),HookMemFileName);<br> Shared:=MapViewOfFile(MemFile,FILE_MAP_WRITE,0,0,0);<br> DLLProc:=@DllEntryPoint;<br>end.<br><br>---------这是*.exe主程序---------------------------<br><br>Program WinHook;<br><br>uses windows,messages,sysutils;<br>{$r *.res} //使用资源文件<br>const<br> HTName:array[1..13] of pchar=(<br> 'CALLWNDPROC','CALLWNDPROCRET','CBT','DEBUG','GETMESSAGE','JOURNALPLAYBACK',<br> 'JOURNALRECORD','KEYBOARD','MOUSE','MSGFILTER','SHELL','SYSMSGFILTER','FOREGROUNDIDLE'<br> );<br>type<br> TWin = record<br> Msg:TMsg;<br> wClass:TWndClass;<br> hMain:integer;<br> hbut,hlab:array[1..16] of integer;<br> hLib:integer;<br> HookStat:array[1..16] of bool;<br> end;<br>var<br> Win:TWin; //结构变量<br> wmhook:integer;<br> WorkPath:string;<br> hRun
rocedure;stdcall;<br>//<br>procedure runhookfun;<br>begin<br> win.hlib:=loadlibrary(pchar(WorkPath+'DemoHook.dll'));<br> if win.hlib=0 then messagebox(win.hmain,'error','',0);<br> hrun:=GetProcAddress(win.hlib,'run');<br> if @hrun<>nil then hrun;<br>end;<br><br>procedure runhook;<br>var tid:integer;<br>begin<br> createthread(nil,0,@runhookfun,nil,0,tid);<br>end;<br><br>function WindowProc(hWnd,Msg,wParam,lParam:longint):LRESULT; stdcall;<br>var k:integer;<br>begin<br> case Msg of<br> WM_SYSCOMMAND:<br> begin<br> case wparam of<br> SC_CLOSE:<br> begin<br> if findwindow('WideHook','WideHook')<>0 then postmessage(findwindow('WideHook','WideHook'),wm_destroy,0,0);<br> end;//showwindow(hwnd,sw_hide);<br> SC_MINIMIZE:;//showwindow(hwnd,sw_hide);<br> SC_MAXIMIZE:;<br> SC_DEFAULT:;<br> SC_MOVE:;<br> SC_SIZE:;<br> //else<br> //Result := DefWindowProc(hwnd, uMsg, wParam, lParam);<br> end;<br> exit;<br> end;<br> wm_command:<br> begin<br> for k:=1 to 13 do<br> begin<br> if (lparam=win.hbut[k]) and ((k=6) or (k=7)) then break;<br> if lparam=win.hbut[k] then<br> begin<br> if win.HookStat[k]=false then postmessage(findwindow('WideHook','WideHook'),wmhook,k,0)<br> else postmessage(findwindow('WideHook','WideHook'),wmhook,-k,0);<br> end;<br> end;<br> end;<br> wm_destroy:<br> begin<br> freelibrary(win.hlib);<br> halt;<br> end;<br> end;<br> if msg=wmhook then<br> begin<br> if wparam>0 then<br> begin<br> setwindowtext(win.hbut[wparam],pchar('stop'));<br> win.HookStat[wparam]:=true;<br> end else<br> begin<br> setwindowtext(win.hbut[-wparam],pchar('start'));<br> win.HookStat[-wparam]:=false;<br> end;<br> end;<br> Result:=DefWindowProc(hWnd,Msg,wParam,lParam);<br>end;<br><br>//主程序的执行函数<br>procedure run;stdcall;<br>var k:integer;<br>begin<br> if findwindow('WinHook',nil)<>0 then exit;<br> win.wClass.hInstance:= hInstance;<br> with win.wclass do<br> begin<br> hIcon:= LoadIcon(hInstance,'MAINICON');<br> hCursor:= LoadCursor(0,IDC_ARROW);<br> hbrBackground:= COLOR_BTNFACE+1;<br> Style:= CS_PARENTDC;<br> lpfnWndProc:= @WindowProc;<br> lpszClassName:='WinHook';<br> end;<br> RegisterClass(win.wClass);<br> win.hmain:=CreateWindow(win.wClass.lpszClassName,'Delphi Hook Demo 2001',WS_VISIBLE or WS_OVERLAPPEDWINDOW,0,0,240,450,0,0,hInstance,nil);<br> for k:=1 to 13 do<br> begin<br> win.hbut[k]:=CreateWindow('BUTTON','Start',WS_VISIBLE or WS_CHILD,10,10+30*(k-1),50,24,win.hmain,0,hInstance,nil);<br> win.hlab[k]:=CreateWindow('STATIC',HTName[k],WS_VISIBLE or WS_CHILD,70,10+30*(k-1)+4,150,24,win.hmain,0,hInstance,nil);<br> win.HookStat[k]:=false;<br> end;<br> WorkPath:=ExtractFilePath(ParamStr(0));<br> runhook;<br> wmhook:=registerwindowmessage(pchar('wm_hook'));<br> while(GetMessage(win.Msg,win.hmain,0,0)) do<br> begin<br> TranslateMessage(win.Msg);<br> DispatchMessage(win.Msg);<br> end;<br>end;<br><br>begin<br> run; //开始运行主程序<br>end.<br>