我给你个例子吧:
mouse hook:
dll:
------------------
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
CommonData;
ZW_MSG : 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);
if (wParam = WM_MOUSEMOVE) then
begin
CommonData.MousePos := pMouse.pt;
//CommonData.hWndCapture := pMouse.hWnd;
//PostMessage(CallBackHandle, 0, 0, 1);
PostMessage(CommonData^.CallBackHandle, ZW_MSG, 0, 1);
//windows.Beep(1000,200);
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);
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;
ZW_MSG := RegisterWindowMessage('WM_ZWNOTIFY');
CommonData := nil;
DLLProc := @DLLMain;
DLLMain(DLL_PROCESS_ATTACH);
end.
======================
exe:
----------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
MapFileUnit;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Mess: TMessage); override;
end;
var
Form1: TForm1;
ZW_MSG : UINT;
HMapFile:THandle;
CommonData
CommonData;
implementation
{$R *.DFM}
function EnableMouseHook(hld:hwnd): boolean; stdcall; external 'MHK.dll';
function DisableMouseHook: boolean; stdcall; external 'MHK.dll';
procedure TForm1.WndProc(var Mess: TMessage);
begin
if (mess.msg = ZW_MSG) then
begin
caption := 's';
if CommonData<>nil then with CommonData^ do
Caption := Format('Mouse Pos: %d, Y : %d',
[MousePos.X,
MousePos.Y]);
end;
inherited;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ZW_MSG := RegisterWindowMessage('WM_ZWNOTIFY');
//CommonData := nil;
MapCommonData(CommonData,HMapFile);
if not(EnableMouseHook(handle)) then ShowMessage('Enable Mouse Hook failed.');
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if not(DisableMouseHook) then ShowMessage('Disable Mouse Hook failed.');
if CommonData<>nil then UnMapCommonData(CommonData,HMapFile);
end;
end.