键盘钩子的共享内存的问题(100)

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

sury9

Unregistered / Unconfirmed
GUEST, unregistred user!
找到一个demo 利用的是写文件到c盘的文本,然后主程序去调用加载,我想高手帮我搞个内存共享的方法把dll里面的信息反馈给主程序 谢谢主程序代码:Function setkeyhook:Boolean;stdcall;external 'Keyspy.dll';Function endkeyhook:Boolean;stdcall;external 'Keyspy.dll';procedure TForm1.Button1Click(Sender: TObject);beginif setkeyhook then begin ShowMessage('钩子安装成功!'); Timer1.Enabled := true; end;end;procedure TForm1.Button2Click(Sender: TObject);begin if EndkeyHook then begin ShowMessage('钩子卸载成功!'); Timer1.Enabled := false; end;end;procedure TForm1.Timer1Timer(Sender: TObject);begin try Memo1.Lines.LoadFromFile('c:/debug.txt'); except exit; end;end;dll代码library keyspy;uses windows, messages, hookproc in 'hookproc.pas';exports //输出的函数setkeyhook,endkeyhook;beginnexthookproc := 0;procsaveexit := exitproc;exitproc := @keyhookexit;end.单元文件代码 unit hookproc;interfaceusesWindows,Messages,SysUtils,Controls,StdCtrls;varnexthookproc: hhook;porcsaveexit: pointer;function setkeyhook: bool;export; //加载钩子function endkeyhook: bool;export; //卸载钩子procedure keyhookexit;far;constafilename= 'c:/debug.txt'; //将键盘输入动作写入文件中vardebugfile: textfile;procsaveexit:pointer;implementationfunction keyboardhookhandler(icode: integer;wparam: wparam; //钩子过程lparam: lparam):lresult;stdcall;export;begin if icode<0 then begin result := callnexthookex(nexthookproc,icode,wparam,lparam); exit; end; assignfile(debugfile,afilename); try append(debugfile); except ReWrite(debugfile); end; if(getkeystate(wparam)and $8000)=$8000 then //获取按键状态 begin if getkeystate(vk_return)<0 then //是否按下回车 begin writeln(debugfile,'回车了'); end else write(debugfile,char(wparam)); //写入文件 end; closefile(debugfile); //关闭文件 result := 0;end;function Setkeyhook: bool;export;begin if nexthookproc= 0 then nexthookproc := SetwindowsHookEx(WH_KEYBOARD,keyboardhookhandler,HInstance,0); //装载钩子 result := nexthookproc<>0;end;function endkeyhook:bool;export;begin if nexthookproc<>0 then begin unhookwindowshookex(nexthookproc); //卸载钩子 nexthookproc := 0; messagebeep(0); end; result := nexthookproc = 0;end;procedure keyhookexit;far;begin if nexthookproc <>0 then endkeyhook; exitproc := procsaveexit;end;end.
 
WM_COPYDATA
 
接受答案了.
 

Similar threads

I
回复
0
查看
649
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
607
import
I
后退
顶部