你见过这样hook的问题吗?(100分)

  • 主题发起人 主题发起人 13380073758
  • 开始时间 开始时间
1

13380073758

Unregistered / Unconfirmed
GUEST, unregistred user!
unit HKProc;<br><br>interface<br> &nbsp;uses Windows, Messages;<br><br><br><br>var<br> &nbsp;hNextHookProc: HHook;<br> &nbsp;procSaveExit: Pointer;<br><br>function KeyboardHookHandler(iCode: Integer;<br> &nbsp;wParam: WPARAM;<br> &nbsp;lParam: LPARAM): LRESULT; stdcall; export;<br>function EnableHotKeyHook(): BOOL; export;<br>function DisableHotKeyHook: BOOL; export;<br>procedure HotKeyHookExit; far;<br><br>implementation<br>var <br> &nbsp; &nbsp;F: TextFile;<br><br>function KeyboardHookHandler(iCode: Integer;<br> &nbsp;wParam: WPARAM;<br> &nbsp;lParam: LPARAM): LRESULT; stdcall; export;<br>const<br> &nbsp;_KeyPressMask = $80000000;<br>begin<br> &nbsp;Result := 0;<br> // postmessage(IlMainHandle,WM_HOOKWIN,iCode,1);<br> &nbsp;//<br> &nbsp;If iCode &lt; 0 Then<br> &nbsp;begin<br> &nbsp; &nbsp;Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br> &nbsp;// 侦测 Ctrl + B 组合键<br> &nbsp;if ((lParam and _KeyPressMask) = 0) and<br> &nbsp; &nbsp;(GetKeyState(vk_Control) &lt; 0) and (wParam = Ord('B')) then<br> &nbsp;begin<br> &nbsp; &nbsp;Result := 1; &nbsp; &nbsp; <br> &nbsp; &nbsp;Writeln(F,'gg');<br> &nbsp; &nbsp;WinExec('Notepad.exe', sw_Normal); &nbsp;// 记事本<br> &nbsp;end;<br>// &nbsp;Writeln(F,inttostr(icode));<br>end;<br><br>function EnableHotKeyHook(): BOOL; export;<br>begin<br> &nbsp;Result := False;<br> &nbsp;if hNextHookProc &lt;&gt; 0 then Exit;<br> &nbsp;// 挂上 WH_KEYBOARD 这型的 HOOK, 同时, 传回值必须保留下<br> &nbsp;// 来, 免得 HOOK 呼叫链结断掉<br> &nbsp;hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,<br> &nbsp; &nbsp;KeyboardHookHandler,<br> &nbsp; &nbsp;HInstance,<br> &nbsp; &nbsp;0);<br> &nbsp;AssignFile(F, 'c:/lll.txt');<br> &nbsp;Rewrite(F);<br> &nbsp;Result := hNextHookProc &lt;&gt; 0;<br>end;<br><br>function DisableHotKeyHook: BOOL; export;<br>begin<br> &nbsp;if hNextHookProc &lt;&gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;UnhookWindowshookEx(hNextHookProc); &nbsp;// 解除 Keyboard Hook<br> &nbsp; &nbsp;hNextHookProc := 0;<br> &nbsp; &nbsp;MessageBeep(0);<br> &nbsp; &nbsp;MessageBeep(0);<br> &nbsp; &nbsp;CloseFile(F);<br> &nbsp;end;<br> &nbsp;Result := hNextHookProc = 0;<br>end;<br><br>procedure HotKeyHookExit;<br>begin<br> &nbsp;// 如果忘了解除 HOOK, 自动代理解除的动作<br> &nbsp;if hNextHookProc &lt;&gt; 0 then DisableHotKeyHook;<br> &nbsp;ExitProc := procSaveExit;<br>end;<br><br>end.<br><br>//=====================================================================<br>library HKTest;<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;SysUtils,<br> &nbsp;Classes,<br> &nbsp;HKProc in 'HKProc.pas';<br><br>{$R *.res}<br><br>exports<br> &nbsp;EnableHotKeyHook,<br> &nbsp;DisableHotKeyHook;<br><br>begin<br> &nbsp;hNextHookProc := 0;<br> &nbsp;procSaveExit := ExitProc;<br> &nbsp;ExitProc := @HotKeyHookExit;<br>end.<br>//==========================================================================<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;Memo1: TMemo;<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);<br> &nbsp; &nbsp;procedure FormCreate(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>function EnableHotKeyHook(): BOOL; external 'HKTEST.DLL';<br>function DisableHotKeyHook: BOOL; external 'HKTEST.DLL';<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> &nbsp;if DisableHotKeyHook then<br> &nbsp; &nbsp;ShowMessage('HotKey Testing..., DONE!!');<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>if EnableHotKeyHook() then<br> &nbsp; &nbsp;ShowMessage('HotKey Testing...');<br>end;<br><br>procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);<br>begin<br> DisableHotKeyHook;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br><br>end;<br><br>end.<br><br>//===========================================================================<br>此程序按 http://www.delphibbs.com/delphibbs/dispq.asp?lid=96332 改动后的结果<br>改动了<br> 1、 var F: TextFile;<br> 2、 增加写向文本<br> &nbsp; &nbsp; &nbsp;Writeln(F,'gg'); &nbsp;//这一行是增加的<br> &nbsp; &nbsp; &nbsp;WinExec('Notepad.exe', sw_Normal); &nbsp;// 记事本<br> 3、增加新建文本文件<br> &nbsp; &nbsp;AssignFile(F, 'c:/lll.txt');<br> &nbsp; &nbsp;Rewrite(F);<br> 4、增加关闭文件<br> &nbsp; &nbsp;CloseFile(F);<br><br>问题是,增加了这几行后hook就不能成功了,有时第一次就会成功,有时一次都不成功,不知道为什么?????????????????????????????????????????????????????????????????????????
 

Similar threads

I
回复
0
查看
762
import
I
后退
顶部