代码如下:
unit UnitHookDLL;
interface
uses Windows, Messages, Dialogs, SysUtils,UnitHookConst,inifiles;
var
hMappingFile : THandle;
pShMem : PShareMem;
FirstProcess : boolean;
NextHook: HHook;
filename:string;
jbinifile:Tinifile;
jb_int:integer;
function StartHook(sender : HWND;MessageID : WORD) : BOOL; stdcall;
function StopHook: BOOL; stdcall;
procedure GetRbutton; stdcall;
implementation
procedure GetRbutton; stdcall;
begin
pShMem^.IfRbutton:=true;
end;
function HookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;
begin
Result := 0;
If iCode < 0 Then Result := CallNextHookEx(NextHook, iCode, wParam, lParam);
case wparam of
WM_LBUTTONUP:
begin
SendMessage(pShMem^.data1[1],WM_User + 129,wParam,integer(@(pShMem^.data2)) );
end;
WM_LBUTTONDOWN:
begin
SendMessage(pShMem^.data1[1],WM_User + 128,wParam,integer(@(pShMem^.data2)) );
end;
WM_LBUTTONDBLCLK:
begin
SendMessage(pShMem^.data1[1],WM_User + 132,wParam,integer(@(pShMem^.data2)) );
end;
WM_RBUTTONDOWN:
begin
SendMessage(pShMem^.data1[1],WM_User + 130,wParam,integer(@(pShMem^.data2)) );
end;
WM_RBUTTONUP:
begin
SendMessage(pShMem^.data1[1],WM_User + 131,wParam,integer(@(pShMem^.data2)) );
end;
WM_NCLBUTTONDBLCLK:
begin
SendMessage(pShMem^.data1[1],WM_User + 132,wParam,integer(@(pShMem^.data2)) );
end;
WM_NCLBUTTONDOWN:
begin
SendMessage(pShMem^.data1[1],WM_User + 128,wParam,integer(@(pShMem^.data2)) );
end;
WM_NCLBUTTONUP:
begin
SendMessage(pShMem^.data1[1],WM_User + 129,wParam,integer(@(pShMem^.data2)) );
end;
WM_NCRBUTTONDOWN:
begin
SendMessage(pShMem^.data1[1],WM_User + 130,wParam,integer(@(pShMem^.data2)) );
end;
WM_RBUTTONDBLCLK:
begin
end;
WM_MBUTTONDOWN:
begin
end;
WM_MBUTTONUP:
begin
end;
WM_MBUTTONDBLCLK:
begin
end;
WM_NCMouseMove, WM_MOUSEMOVE:
begin
end;
end;
end;
function StartHook(sender : HWND;MessageID : WORD) : BOOL;
function GetModuleHandleFromInstance: THandle;
var
s: array[0..512] of char;
begin
GetModuleFileName(hInstance, s, sizeof(s)-1);
Result := GetModuleHandle(s);
end;
begin
Result := False;
if NextHook <> 0 then Exit;
pShMem^.data1[1]:=sender;
pShMem^.data1[2]:=messageid;
NextHook :=SetWindowsHookEx(WH_mouse, HookHandler, HInstance, 0); //全局
Result := NextHook <> 0;
end;
function StopHook: BOOL;
begin
if NextHook <> 0 then
begin
UnhookWindowshookEx(NextHook);
NextHook := 0;
end;
Result := NextHook = 0;
end;
initialization
hMappingFile := OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);
if hMappingFile=0 then
begin
hMappingFile := CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShareMem),MappingFileName);
FirstProcess:=true;
end
else FirstProcess:=false;
if hMappingFile=0 then Exception.Create('不能建立共享内存!');
pShMem := MapViewOfFile(hMappingFile,FILE_MAP_WRITE or FILE_MAP_READ,0,0,0);
if pShMem = nil then
begin
CloseHandle(hMappingFile);
Exception.Create('不能映射共享内存!');
end;
if FirstProcess then
begin
pShmem^.IfRbutton := false;
end;
NextHook:=0;
finalization
UnMapViewOfFile(pShMem);
CloseHandle(hMappingFile);
end.