为什么我的全局鼠标钩子(内附源程序), 在鼠标经过滚动条的时候什么消息都收不到? (100分)

  • 主题发起人 主题发起人 hejiesi
  • 开始时间 开始时间
H

hejiesi

Unregistered / Unconfirmed
GUEST, unregistred user!
请问怎样解决?
library MHK;

{ 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
//SysUtils,
windows,
messages,
//Classes,
MapFileUnit in 'MapFileUnit.pas';

var
hNextHookProc: HHook;
HMapFile:THandle;
CommonData:pCommonData;
WM_Global_MBUTTONDOWN: UINT;
WM_Global_MBUTTONUP: UINT;
WM_Global_MOUSEMOVE: UINT;
WM_Global_MOUSEWHEEL: UINT;

{$R *.RES}

{================== Install Mouse Hook Support ==============================}
function MousePosHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
pMouse : PMOUSEHOOKSTRUCT;
begin

if iCode < 0 then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
windows.Beep(1000,200);
exit;
end;

pMouse := PMOUSEHOOKSTRUCT(lParam);

CommonData^.MousePos := pMouse.pt;
case wParam of
WM_MBUTTONDOWN : begin
PostMessage(CommonData^.CallBackHandle, WM_Global_MBUTTONDOWN, 0, 1);
end;

WM_MBUTTONUP : begin
PostMessage(CommonData^.CallBackHandle, WM_Global_MBUTTONUP, 0, 1);
end;

WM_MOUSEMOVE : begin
PostMessage(CommonData^.CallBackHandle, WM_Global_MOUSEMOVE, 0, 1);
end;

WM_MOUSEWHEEL : begin
CommonData^.Direction := HIWORD(wParam);
PostMessage(CommonData^.CallBackHandle, WM_Global_MOUSEWHEEL, 0, 1);
end;

end;

Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
end;

function EnableMouseHook(hld:hwnd): BOOL; stdcall;export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;

hNextHookProc := SetWindowsHookEx(WH_MOUSE, MousePosHookHandler,Hinstance, 0);

if CommonData <> nil then
begin
CommonData^.CallBackHandle := hld;
//CommonData^.CallBackProcID := ProcessID;
end;

Result :=hNextHookProc <> 0 ;
end;

function DisableMouseHook: BOOL; stdcall;export;
begin
try
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc := 0;
end;
Result := hNextHookProc = 0;
except
MessageBeep(0);
Result := False;
end;
end;

procedure DLlMain(dwReason: DWORD);
begin
case dwReason of

DLL_PROCESS_ATTACH:
begin
HMapFile:=0;
MapCommonData(CommonData,HMapFile);
end; // DLL_PROCESS_ATTACH:

DLL_PROCESS_DETACH:
begin
if CommonData<>nil then UnMapCommonData(CommonData,HMapFile);
end; // DLL_PROCESS_DETACH:

end; //calse
end;



exports
EnableMouseHook,
DisableMouseHook;

begin
hNextHookProc := 0;
WM_Global_MBUTTONDOWN := RegisterWindowMessage('WM_Global_MBUTTONDOWN');
WM_Global_MBUTTONUP := RegisterWindowMessage('WM_Global_MBUTTONUP');
WM_Global_MOUSEMOVE := RegisterWindowMessage('WM_Global_MOUSEMOVE');
WM_Global_MOUSEWHEEL := RegisterWindowMessage('WM_Global_MOUSEWHEEL');
CommonData := nil;
DLLProc := @DLLMain;
DLLMain(DLL_PROCESS_ATTACH);
end.
 
hNextHookProc也要放在共享内存里
 
to weiwei81123:

能不能说一下具体怎样做吗?

我忘了还有一部分代码没有放上去

unit MapFileUnit;

interface
uses
windows;
type
pCommonData = ^TCommonData;
TCommonData = record
CallBackHandle:HWnd;
Direction: short;
MousePos : TPoint; //Current Mouse Positoin
end;

procedure MapCommonData(var CommonData:pCommonData;var HMapFile:THandle);
procedure UnMapCommonData(var CommonData:pCommonData;var HMapFile:THandle);

implementation

procedure MapCommonData(var CommonData:pCommonData;var HMapFile:THandle);
var FirstCall: Boolean;
begin
HMapFile:=OpenFileMapping(FILE_MAP_WRITE, False, 'Global_MOUSE');
FirstCall:=(HMapFile = 0);
if FirstCall then
HMapFile:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,
0,SizeOf(TCommonData),
'Global_MOUSE');
CommonData:= MapViewOfFile(HMapFile, FILE_MAP_WRITE, 0, 0, 0);
if FirstCall then FillChar(CommonData^, SizeOf(TCommonData), 0);
end;

procedure UnMapCommonData(var CommonData:pCommonData;var HMapFile:THandle);
begin
try
UnMapViewOfFile(CommonData);
CommonData := nil;
CloseHandle(HMapFile);
HMapFile := 0;
except
MessageBox(0,
'Error when free MapViewFile',
'Global Hook Error',
MB_OK);
end; //try
end;

end.
 
我曾经这样做过:文件映射是先在函数1中Create ,写入数据,在函数2中Open,结果无法得到数据
后来我在函数1中Create ,写入数据,在函数2中也Create(文件名相同),再读取数据,就可以得到数据
你要不要试一下
 
To yahle:
不太明白跟文件映射之间的关系, 我现在的问题是在鼠标经过滚动条的时候完全收不到消息, 如果是文件映射的问题, 哪我最多得不到消息的具体数据, 但怎样也收到消息吧.
 
哇,好长,好我没写了,
 
把hNextHookProc放进TCommonData结构
 
to weiwei81123:

我把TCommonData 的结构改成
TCommonData = record
hNextHookProc: HHook;
CallBackHandle:HWnd;
Direction: short;
MousePos : TPoint; //Current Mouse Positoin
end;

然后把library MHK 里面的hNextHookProc 全部改成 CommonData^.hNextHookProc 但程序一起动就出错, 请问是什么原因, 应该怎样做, 不好意思麻烦你这么多次.
 
因为初始化顺序不对,必须先执行了DllMain初始化CommonData之后再初始化CommonData^.hNextHookProc
 
我修改了顺序 改成
WM_Global_MBUTTONDOWN := RegisterWindowMessage('WM_Global_MBUTTONDOWN');
WM_Global_MBUTTONUP := RegisterWindowMessage('WM_Global_MBUTTONUP');
WM_Global_MOUSEMOVE := RegisterWindowMessage('WM_Global_MOUSEMOVE');
WM_Global_MOUSEWHEEL := RegisterWindowMessage('WM_Global_MOUSEWHEEL');
CommonData := nil;
DLLProc := @DLLMain;
DLLMain(DLL_PROCESS_ATTACH);
CommonData^.hNextHookProc := 0;
虽然没有了错误,但问题还是没有解决, 所以能不能请你再详细一点吗, 先谢过了
 
我也想知道。。。
 
to blindlf: 已经在另外一条问题那里给了280分给你了. 你查看一下吧
 
终于有分了。哈
 
这个问题已经解决 请看 http://www.delphibbs.com/delphibbs/dispq.asp?lid=2504188
 
恭喜!
我來領分.
 
兄弟,我还没有明白你是怎么解决的呀,给个DEMO呀。zzjmail@163.com
 
To zzjmail: 请看 http://www.delphibbs.com/delphibbs/dispq.asp?lid=2504188
 
后退
顶部