谁能给我看看我的hook代码错在哪里,最基本的hook
我想得到鼠标所到之处的窗口内容,但是我只现在只能得到自身窗口上的内容为什么?
代码如下:
unit main;
interface
uses windows,Messages;
procedure enablehook;export;
procedure getmouse(var ax,ay:integer; var Apsw:pchar);export;
function mousehook(ncode:integer;wParam:wparam;lparam:pointer):LRESULT;stdcall;
procedure disenablehook;export;
var
newhook:hhook=0;
mousepos:Tpoint;
psw:array[0..255] of char;
implementation
procedure enablehook;export;
begin
if newhook=0 then
newhook:=setwindowshookex(WH_MOUSE,@mousehook,HINSTANCE,0);
end;
function mousehook(ncode:integer;wParam:wparam;lparam:Pointer):LRESULT;
var
l:^mousehookstruct;
begin
result:=0;
l:=lparam;
mousepos:=l.pt;
if ncode<0then
result:=callnexthookex(newhook,ncode,wparam,integer(lparam))
else
begin
sendmessage(windowfrompoint(l.pt),wm_gettext,255,integer(@psw));
end;
end;
procedure disenablehook;
begin
if newhook<>0 then
unhookwindowshookex(newhook);
end;
procedure getmouse(var ax,ay:integer; var Apsw:pchar);
begin
ax:=mousepos.X;
ay:=mousepos.Y;
Apsw:=psw;
end;
end.
上面是dll文件的代码
下面是exe文件的代码:
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
isstop:bool;
{ Private declarations }
public
{ Public declarations }
end;
procedure enablehook;export;external 'mousehook.dll';
procedure disenablehook;export;external 'mousehook.dll';
procedure getmouse(var ax,ay:integer;var apsw:pchar);export;external 'mousehook.dll';
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
lx,ly:integer;
lpsw:pchar;
begin
EnableHook;
getmouse(lx,ly,lpsw);
isStop:=false;
while not isStop do
begin
GetMouse(lx,ly,lpsw);
Edit1.Text:=String(lx)+','+String(ly);
Edit2.Text:=lpsw;
Application.ProcessMessages;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
isstop:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
isstop:=true;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
isstop:=true;
disenablehook;
end;
end.
现在问题是我只要鼠标在本身的窗体上就可以正确显示
但是鼠标一出这个窗体就不能显示了
为什么?