C
cqwty
Unregistered / Unconfirmed
GUEST, unregistred user!
我写的一个钩子程序,准备拦截指定的窗体的WM_Enable消息,关键代码如下:<br>钩子部分代码(从论坛上copy的),所以连名字都还没有修改。<br>library MouseHook;<br>uses<br> MouseIdle in 'MouseIdle.pas';<br>Exports<br> CreateHook,<br> RemoveHook;<br>{$R *.res}<br>begin<br> whHelp := 0;<br> procSaveExit := ExitProc;<br> ExitProc := @HelpHookExit;<br>end.<br>MouseIdle.pas代码:<br>unit MouseIdle;<br>interface<br>uses<br> Windows, Messages, SysUtils;<br>var<br> whHelp: HHook;<br> procSaveExit: Pointer;<br> function CreateHook(HookHandle:LongWORD): Boolean; export;<br> function RemoveHook: Boolean; export;<br> procedure HelpHookExit; far;<br> function HelpProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;<br>implementation<br>function CreateHook(HookHandle:LongWORD): Boolean;<br>//HookHandle主要是指定要拦截的窗体句柄<br>var<br> ThreadID:LongWord;//获得拥有该窗体的线程句柄<br>begin<br> Result := false;<br> if whHelp <> 0 then Exit;<br> ThreadID := GetWindowThreadProcessId(HookHandle, nil);<br> whHelp := SetWindowsHookEx(WH_CALLWNDPROC, @HelpProc, Hinstance, ThreadID);<br> Result := whHelp <> 0;<br>end;<br><br>function RemoveHook: Boolean;<br>begin<br> if whHelp <> 0 then<br> begin<br> UnHookWindowsHookEx(whHelp);<br> whHelp := 0;<br> end;<br> Result := whHelp = 0;<br>end;<br><br>procedure HelpHookExit;<br>begin<br> if whHelp <> 0 then<br> RemoveHook;<br> Exitproc := procSaveExit;<br>end;<br><br>function HelpProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;<br>var<br> HandleStr : string;<br>begin<br> Result := 0;<br> if iCode < 0 then Result := CallNextHookEx(whHelp, iCode, wParam, lParam);<br> if pcwpstruct(lParam)^.message = WM_enable then<br>//如果拦截到的消息是WM_Enable,那么做如下的处理<br> begin<br> HandleStr := inttostr(pcwpstruct(lparam)^.hwnd);<br> Messagebox(0, Pchar('click on:'+HandleStr), 'Hook', 0+64);<br> end;<br>end;<br>end.<br>调用dll的project的代码:<br>procedure TSpyWindowForm.Button1Click(Sender: TObject);<br>begin<br> CreateHook(13894624);//对指定的窗体安装钩子<br>end;<br><br>procedure TSpyWindowForm.Button2Click(Sender: TObject);<br>begin<br> RemoveHook;//卸载钩子<br>end;<br>劳烦高手帮我看看怎么回事,总是在启动钩子的时候拦截到程序本身的窗口的消息,以及application的消息,而且还有出错的地方,郁闷了。