学习HOOK中遇到的问题,请高手赐教(50分)

  • 主题发起人 主题发起人 sy0116
  • 开始时间 开始时间
S

sy0116

Unregistered / Unconfirmed
GUEST, unregistred user!
我在EXE文件中调用这个DLL,但是当我运行程序并按下任意一个键之后发现一个奇怪的现象:对话框“ok”和“good”会连续跳出来两次,假设我现在在“记事本”中按下键,此时对话框“ok”和“good”会连续跳出来两次,然后察看c:/t3.txt发现这个文件中记录下<br>“Notepad<br> &nbsp;无标题-记事本”<br>而我想要的只是“无标题-记事本”,是不是title中也包含了窗体类名呢?而且还有一个严重问题,同样假设在记事本中按键,这时如果不关闭记事本,但调用hookend,就会出现“内存不能为Read的错误提示”,在其他任何程序除了project1.exe(project1.exe就是调用DLL文件的EXE文件)都会如此。请问这些问题是何原因引起的,应该如何解决<br>library keyhook1;<br><br>{ Important note about DLL memory management: ShareMem must be the<br> &nbsp;first unit in your library's USES clause AND your project's (select<br> &nbsp;Project-View Source) USES clause if your DLL exports any procedures or<br> &nbsp;functions that pass strings as parameters or function results. This<br> &nbsp;applies to all strings passed to and from your DLL--even those that<br> &nbsp;are nested in records and classes. ShareMem is the interface unit to<br> &nbsp;the BORLNDMM.DLL shared memory manager, which must be deployed along<br> &nbsp;with your DLL. To avoid using BORLNDMM.DLL, pass string information<br> &nbsp;using PChar or ShortString parameters. }<br><br>uses<br> &nbsp;ShareMem,<br> &nbsp;Windows,<br> &nbsp;Dialogs,<br> &nbsp;SysUtils;<br><br><br>var<br> &nbsp;oldhook:hhook;<br> &nbsp;hd:HWND;<br> &nbsp;title:array [0..255] of Char;<br>{$R *.res}<br>function wfile(name,content:string):Boolean;stdcall;//用文件记录键盘按键<br>var<br> &nbsp;f:TextFile;<br>begin<br> &nbsp;Result:=False;<br> &nbsp;begin<br> &nbsp; &nbsp;AssignFile(F,name);<br> &nbsp; &nbsp;if FileExists(name) then<br> &nbsp; &nbsp;Append(f)<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp;Rewrite(f);<br> &nbsp; &nbsp;Writeln(f,content);<br> &nbsp; &nbsp;Result:=True;<br> &nbsp; &nbsp;CloseFile(f);<br> &nbsp;end;<br>end;<br><br>function HookProc(ncode,wparam,lparam:Integer):Integer;stdcall;<br>var<br> &nbsp;winstr:HWND;<br>begin<br> &nbsp;if wparam&gt;=0 then<br> &nbsp;ShowMessage('ok');<br> &nbsp;begin<br> &nbsp; &nbsp;winstr:=GetForegroundWindow;<br> &nbsp; &nbsp;hd:=winstr;<br> &nbsp; &nbsp;GetWindowText(hd,@title,256);<br> &nbsp; &nbsp;if wfile('c:/t3.txt',title) then ShowMessage('Good');<br> &nbsp;end;<br> &nbsp;Result:=CallNextHookEx(oldhook,nCode,wParam,lParam);<br>end;<br><br>function sethook:Boolean;stdcall;<br>begin<br> &nbsp;oldhook:=SetWindowsHookEx(WH_KEYBOARD,@HookProc,HInstance,0);<br> &nbsp;Result:=True;<br>end;<br><br>function hookend:Boolean;stdcall;<br>begin<br> &nbsp;UnhookWindowsHookEx(oldhook);<br> &nbsp;Result:=True;<br>end;<br>exports<br> &nbsp;sethook,hookend;<br>begin<br>end.
 
正是怪事啊,我把这个DLL文件重新保存了一个成一个hook1.dll但代码完全没变再测试却发现不再有“不能为Read”的提示了。<br>但是,出现两次对话框的问题还是没解决
 
一次按键分为按下与弹起两个消息,所以会出现两次
 
有什么办法只得到按下的消息呢?
 
多人接受答案了。
 
后退
顶部