你编的程序基本上没有什么问题,我仔细研究了你的程序,我最大的疑点就是,你的log()函数,可能覆盖了以前的内容.<br>另外为了安全,最好在用事件之前,先判断一下icode;如果小于0;就不允许做处理就返回.<br>下面是我仿造你的库改动了一下.目的是做个示范:记得要推出程序的时候要调用:unhook过程;<br>unit udll;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br>var<br> myhook:Thandle;<br>procedure starthook;stdcall;export;<br>procedure unhook;stdcall;export;<br>function myHookProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM):LRESULT;stdcall;<br>procedure log(ss:string);<br><br><br>implementation<br>procedure starthook;stdcall;<br>begin<br>myhook:=SetWindowsHookEx(WH_MOUSE,myHookProc,hInstance,0);<br>end;<br><br>function myHookProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM):LRESULT;stdcall;<br>var<br> X, Y : Integer;<br>begin<br>result:=0;<br>if iCode<0 then //为了hook的安全此处应该判断...<br> begin<br> result := CallNextHookEx(myHook, iCode, wParam, lParam);<br> exit;<br> end;<br>if (wParam = WM_LBUTTONDOWN) or (wParam=WM_NCLBUTTONDOWN) then<br>begin<br> X := PMouseHookStruct(lParam).pt.X;<br> Y := PMouseHookStruct(lParam).pt.Y;<br> log(#13+#10+'leftbutton click at' +'['+ inttostr(X) + ';' + inttostr(Y)+']');<br>end;<br>if (wParam = WM_NCMouseMove) or (wParam=WM_MOUSEMOVE) then<br>begin<br> X := PMouseHookStruct(lParam).pt.X;<br> Y := PMouseHookStruct(lParam).pt.Y;<br> log(' ['+inttostr(X) + ';' + inttostr(Y)+']');<br>end;//一样的嘛,怎么会捕捉不到呢.<br><br><br>result := CallNextHookEx(myHook, iCode, wParam, lParam);<br>end;<br>procedure unhook;stdcall;export;<br>begin<br> unhookwindowshookex(myhook);<br>end;<br>procedure log(ss:string);<br>var<br> file1:Tmemorystream;<br>begin<br> try<br> file1:=tmemorystream.Create;<br> try<br> file1.LoadFromFile('e:/delphi/log.txt');<br> except<br> end;<br> file1.Position:=file1.size;<br> file1.Write(ss[1],length(ss));<br> file1.SaveToFile('e:/delphi/log.txt');<br> finally<br> file1.free;<br> end;<br>end;<br>end.<br><br><br>library prjdll;<br><br>uses<br> udll in 'udll.pas';<br><br>{$R *.RES}<br><br>exports<br> starthook,unhook;<br>begin<br>end.<br><br>