S
sy0116
Unregistered / Unconfirmed
GUEST, unregistred user!
我在EXE文件中调用这个DLL,但是当我运行程序并按下任意一个键之后发现一个奇怪的现象:对话框“ok”和“good”会连续跳出来两次,假设我现在在“记事本”中按下键,此时对话框“ok”和“good”会连续跳出来两次,然后察看c:/t3.txt发现这个文件中记录下<br>“Notepad<br> 无标题-记事本”<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> first unit in your library's USES clause AND your project's (select<br> Project-View Source) USES clause if your DLL exports any procedures or<br> functions that pass strings as parameters or function results. This<br> applies to all strings passed to and from your DLL--even those that<br> are nested in records and classes. ShareMem is the interface unit to<br> the BORLNDMM.DLL shared memory manager, which must be deployed along<br> with your DLL. To avoid using BORLNDMM.DLL, pass string information<br> using PChar or ShortString parameters. }<br><br>uses<br> ShareMem,<br> Windows,<br> Dialogs,<br> SysUtils;<br><br><br>var<br> oldhook:hhook;<br> hd:HWND;<br> title:array [0..255] of Char;<br>{$R *.res}<br>function wfile(name,content:string):Boolean;stdcall;//用文件记录键盘按键<br>var<br> f:TextFile;<br>begin<br> Result:=False;<br> begin<br> AssignFile(F,name);<br> if FileExists(name) then<br> Append(f)<br> else<br> Rewrite(f);<br> Writeln(f,content);<br> Result:=True;<br> CloseFile(f);<br> end;<br>end;<br><br>function HookProc(ncode,wparam,lparam:Integer):Integer;stdcall;<br>var<br> winstr:HWND;<br>begin<br> if wparam>=0 then<br> ShowMessage('ok');<br> begin<br> winstr:=GetForegroundWindow;<br> hd:=winstr;<br> GetWindowText(hd,@title,256);<br> if wfile('c:/t3.txt',title) then ShowMessage('Good');<br> end;<br> Result:=CallNextHookEx(oldhook,nCode,wParam,lParam);<br>end;<br><br>function sethook:Boolean;stdcall;<br>begin<br> oldhook:=SetWindowsHookEx(WH_KEYBOARD,@HookProc,HInstance,0);<br> Result:=True;<br>end;<br><br>function hookend:Boolean;stdcall;<br>begin<br> UnhookWindowsHookEx(oldhook);<br> Result:=True;<br>end;<br>exports<br> sethook,hookend;<br>begin<br>end.