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