主题:自已写的一个[HOOK DLL]遇到点问题,望高手帮分析一下~~~
我写的是一个常规的DLL程序,分两部分:
Hook.dpr文件里面的代码:
Library MyHook;
uses
HookPAS in 'Hook.pas';
exports
LoadHook,
UnloadHook;
begin
hNextHookProc := 0;
procSaveExit := ExitProc;
ExitProc := @HotKeyHookExit;
end.
Hook.pas文件里的代码:
interface
uses
Windows, Messages;
var
hNextHookProc: HHook;
procSaveExit: Pointer;
function KeyboardHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
export;
function LoadHook: BOOL; export;
function UnloadHook: BOOL; export;
procedure HotKeyHookExit; far;
implementation
function KeyboardHookHandler(iCode: Integer;wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
const
_KeyPressMask = $80000000;
begin
Result := 0;
If iCode <0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
if ((lParam and _KeyPressMask) = 0) and (GetKeyState(vk_Control) <0) and (wParam = ord('f'))then // =======> 问题在这里: 我是想检测是否按下ctrl + f 键,编出来怎么不行?
begin
Result := 1;
WinExec('C:\qq.exe', sw_Normal);
end
end;
function LoadHook: BOOL; export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,KeyboardHookHandler,HInstance,0);
Result := hNextHookProc <> 0;
end;
function UnloadHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;
procedure HotKeyHookExit;
begin
if hNextHookProc <> 0 then Unloadhook;
ExitProc := procSaveExit;
end;
end.
另外,还有一个严重的问题想请教一下各位大虾,就是我这个代码是网上抄回来自己加工的,很多其实自己读不懂,比如像hNextProc这样的东西到底是用来代表什么的(不是自己声明的变量吗?在VAR后面的啊,怎么连自己也不知道用来干嘛的啊,难道是类里面封装好的?还是库单元里面包含有的?),还有,如果我想再在这个DLL里加多个接口Loadhook2和Unloadhook2,让这两个接口里面又有别的动作执行,我该怎么写代码啊????希望高手赐教~~~~~~
Hook.dpr文件里面的代码:
Library MyHook;
uses
HookPAS in 'Hook.pas';
exports
LoadHook,
UnloadHook;
begin
hNextHookProc := 0;
procSaveExit := ExitProc;
ExitProc := @HotKeyHookExit;
end.
Hook.pas文件里的代码:
interface
uses
Windows, Messages;
var
hNextHookProc: HHook;
procSaveExit: Pointer;
function KeyboardHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
export;
function LoadHook: BOOL; export;
function UnloadHook: BOOL; export;
procedure HotKeyHookExit; far;
implementation
function KeyboardHookHandler(iCode: Integer;wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
const
_KeyPressMask = $80000000;
begin
Result := 0;
If iCode <0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
if ((lParam and _KeyPressMask) = 0) and (GetKeyState(vk_Control) <0) and (wParam = ord('f'))then // =======> 问题在这里: 我是想检测是否按下ctrl + f 键,编出来怎么不行?
begin
Result := 1;
WinExec('C:\qq.exe', sw_Normal);
end
end;
function LoadHook: BOOL; export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;
hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,KeyboardHookHandler,HInstance,0);
Result := hNextHookProc <> 0;
end;
function UnloadHook: BOOL; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc := 0;
MessageBeep(0);
MessageBeep(0);
end;
Result := hNextHookProc = 0;
end;
procedure HotKeyHookExit;
begin
if hNextHookProc <> 0 then Unloadhook;
ExitProc := procSaveExit;
end;
end.
另外,还有一个严重的问题想请教一下各位大虾,就是我这个代码是网上抄回来自己加工的,很多其实自己读不懂,比如像hNextProc这样的东西到底是用来代表什么的(不是自己声明的变量吗?在VAR后面的啊,怎么连自己也不知道用来干嘛的啊,难道是类里面封装好的?还是库单元里面包含有的?),还有,如果我想再在这个DLL里加多个接口Loadhook2和Unloadhook2,让这两个接口里面又有别的动作执行,我该怎么写代码啊????希望高手赐教~~~~~~