以下是我HOOK DLL单元的代码<br>unit untWinHook;<br>interface<br>uses<br> Windows, Messages;<br>type<br> PMyDLLVar = ^TMyDLLVar;<br> TMyDLLVar = record<br> hTrayHook, hShellHook, hCBTHook,<br> hKeyHook, hMouseHook : HHOOK; //HOOK句柄<br> Msg: TMessage;<br> end;<br><br>type<br> THookType=(htSHELL,htKEYBOARD,htMOUSE,<br> htCBT,htGETMESSAGE,htCALLWNDPROC,<br> htMSGFILTER,htSYSMSGFILTER);<br><br> //安装钩子<br> procedure InstallHook(SpyWindow : HWND; HookType:THookType); stdcall;<br> //卸载钩子<br> Procedure UnInstallHook(HookType:THookType); stdcall;<br>var<br> SHELL_EVENT : UINT;<br> CBT_EVENT : UINT;<br> KEY_EVENT : UINT;<br> MOUSE_EVENT : UINT;<br><br>implementation<br><br>function ShellHookProc(nCode: Integer; wParam: WPARAM;lParam:LPARAM): LRESULT; stdcall;<br>begin<br> Result := CallNextHookEx(DLLData^.hShellHook, nCode, wParam, lParam);<br>end;<br><br>function CBTHookProc(nCode: Integer; wParam : WPARAM; lParam : LPARAM): LRESULT; stdcall;<br>begin<br> Result := CallNextHookEx(DLLData^.hCBTHook, nCode, wParam, lParam);<br>end;<br><br>function KeyHookProc( nCode: Integer; wParam: WPARAM; lParam:LPARAM <br> ): LRESULT; stdcall;<br>begin<br> Result := CallNextHookEx(DLLData^.hKeyHook, nCode, wParam, lParam);<br>end;<br><br>function MouseHookProc( nCode:Integer; wParam:WPARAM;<br> lParam:LPARAM):LRESULT; stdcall;<br>begin<br> Result := CallNextHookEx(DLLData^.hMouseHook, nCode, wParam, lParam);<br>end;<br><br>//安装钩子<br>procedure InstallHook(SpyWindow : HWND; HookType : THookType); stdcall;<br>begin<br> With DLLData^ do<br> Case HookType of<br> htSHELL : begin<br> if (hShellHook = 0) then<br> hShellHook := SetWindowsHookEx(WH_SHELL, @ShellHookProc, HInstance , 0);<br> end;<br> htCBT : begin<br> if (hCBTHook = 0) then<br> hCBTHook :=SetWindowsHookEx(WH_CBT, @CBTHookProc, HInstance , 0);<br> end;<br> htKEYBOARD :<br> begin<br> if (hKeyHook=0) then<br> hKeyHook:=SetWindowsHookEx(WH_KEYBOARD, @KeyHookProc, HInstance , 0);<br> end;<br> htMouse :<br> begin<br> if (hMouseHook=0) then<br> hMouseHook:=SetWindowsHookEx(WH_MOUSE, @MouseHookProc,hInstance,0);<br> end;<br> end;<br>end;<br><br>Procedure UnInstallHook(HookType:THookType); stdcall;<br>begin<br> with DLLData^ do<br> case HookType of<br> htSHELL: begin<br> if (hShellHook<>0) then<br> begin<br> UnhookWindowsHookEx(hShellHook);<br> hShellHook:=0;<br> end;<br> end;<br> htCBT : begin<br> if (hCBTHook<>0) then<br> begin<br> UnhookWindowsHookEx(hCBTHook);<br> hCBTHook:=0;<br> end;<br> end;<br> htKEYBOARD: begin<br> if (hKeyHook<>0) then<br> begin<br> UnhookWindowsHookEx(hKeyHook);<br> hKeyHook:=0;<br> end;<br> end;<br> htMouse : begin<br> if (hMouseHook <>0) then<br> begin<br> UnHookWindowsHookEx(hMouseHook);<br> hMouseHook:=0;<br> end;<br> end;<br> end;<br>end;<br>end.