我在EXE文件中调用这个DLL,但是当我运行程序并按下任意一个键之后发现一个奇怪的现象:对话框“ok”和“good”会连续跳出来两次,假设我现在在“记事本”中按下键,此时对话框“ok”和“good”会连续跳出来两次,然后察看c:\t3.txt发现这个文件中记录下
“Notepad
无标题-记事本”
而我想要的只是“无标题-记事本”,是不是title中也包含了窗体类名呢?而且还有一个严重问题,同样假设在记事本中按键,这时如果不关闭记事本,但调用hookend,就会出现“内存不能为Read的错误提示”,在其他任何程序除了project1.exe(project1.exe就是调用DLL文件的EXE文件)都会如此。请问这些问题是何原因引起的,应该如何解决
library keyhook1;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
ShareMem,
Windows,
Dialogs,
SysUtils;
var
oldhook:hhook;
hd:HWND;
title:array [0..255] of Char;
{$R *.res}
function wfile(name,content:string):Boolean;stdcall;//用文件记录键盘按键
var
f:TextFile;
begin
Result:=False;
begin
AssignFile(F,name);
if FileExists(name) then
Append(f)
else
Rewrite(f);
Writeln(f,content);
Result:=True;
CloseFile(f);
end;
end;function HookProc(ncode,wparam,lparam:Integer):Integer;stdcall;
var
winstr:HWND;
begin
if wparam>=0 then
ShowMessage('ok');
begin
winstr:=GetForegroundWindow;
hd:=winstr;
GetWindowText(hd,@title,256);
if wfile('c:\t3.txt',title) then ShowMessage('Good');
end;
Result:=CallNextHookEx(oldhook,nCode,wParam,lParam);
end;function sethook:Boolean;stdcall;
begin
oldhook:=SetWindowsHookEx(WH_KEYBOARD,@HookProc,HInstance,0);
Result:=True;
end;function hookend:Boolean;stdcall;
begin
UnhookWindowsHookEx(oldhook);
Result:=True;
end;
exports
sethook,hookend;
begin
end.
“Notepad
无标题-记事本”
而我想要的只是“无标题-记事本”,是不是title中也包含了窗体类名呢?而且还有一个严重问题,同样假设在记事本中按键,这时如果不关闭记事本,但调用hookend,就会出现“内存不能为Read的错误提示”,在其他任何程序除了project1.exe(project1.exe就是调用DLL文件的EXE文件)都会如此。请问这些问题是何原因引起的,应该如何解决
library keyhook1;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
ShareMem,
Windows,
Dialogs,
SysUtils;
var
oldhook:hhook;
hd:HWND;
title:array [0..255] of Char;
{$R *.res}
function wfile(name,content:string):Boolean;stdcall;//用文件记录键盘按键
var
f:TextFile;
begin
Result:=False;
begin
AssignFile(F,name);
if FileExists(name) then
Append(f)
else
Rewrite(f);
Writeln(f,content);
Result:=True;
CloseFile(f);
end;
end;function HookProc(ncode,wparam,lparam:Integer):Integer;stdcall;
var
winstr:HWND;
begin
if wparam>=0 then
ShowMessage('ok');
begin
winstr:=GetForegroundWindow;
hd:=winstr;
GetWindowText(hd,@title,256);
if wfile('c:\t3.txt',title) then ShowMessage('Good');
end;
Result:=CallNextHookEx(oldhook,nCode,wParam,lParam);
end;function sethook:Boolean;stdcall;
begin
oldhook:=SetWindowsHookEx(WH_KEYBOARD,@HookProc,HInstance,0);
Result:=True;
end;function hookend:Boolean;stdcall;
begin
UnhookWindowsHookEx(oldhook);
Result:=True;
end;
exports
sethook,hookend;
begin
end.
WH_KEYBOARD在keydown和keyup都会触发,所以你按一个键会有两次触发
2.Notepad
无标题-记事本:
Notepad应该是弹出的对话框的标题
3.出现“内存不能为Read的错误提示”:
不知道