DLL 代码:<br>library Mhook;<br><br>uses<br>Main in 'Main.pas',<br><br>windows,<br>messages,<br>Classes,<br>sysutils;<br><br>procedure DLLEntryPoint(dwReason: DWORD);<br>begin<br>case dwReason of<br> DLL_PROCESS_ATTACH: OpenShareData;<br> DLL_PROCESS_DETACH: CloseShareData;<br>end;<br>end;<br>exports<br>EnableHook,<br>DisableHook;<br>begin<br><br>procExit := ExitProc;<br>ExitProc := @HookExit;<br>DLLProc := @DLLEntryPoint;<br>DLLEntryPoint(DLL_PROCESS_ATTACH);<br>end.<br>=======================================================================<br><br>unit Main;<br><br>interface<br>uses<br>windows,<br>messages,<br>Classes,<br>sysutils;<br><br>type<br><br>PServerData = ^ServerData;<br>ServerData = record<br> Wnd: HWND;<br> UniqueMsgID: Cardinal;<br> hHk: HHOOK;<br>end;<br><br>var<br><br>procExit: pointer;<br>SData: PServerData;<br>MapServerHandle: THandle;<br>const<br><br>cMMServerData: PChar = 'ServerMapData';<br><br><br>function EnableHook(h: HWND): Boolean; stdcall;<br>function DisableHook(): Boolean; stdcall;<br><br>procedure HookExit; far;<br>procedure OpenShareData();<br>procedure CloseShareData();<br><br>function GetMsgProc(nCode: integer; wParam, lParam: longint): LRESULT; stdcall;<br><br>implementation<br><br>procedure OpenShareData();<br>var<br>Size: integer;<br>begin<br>Size := Sizeof(SData);<br>MapServerHandle := CreateFileMapping(DWORD(-1), nil, PAGE_READWRITE, 0, Size,<br> cMMServerData);<br>SData := MapViewOfFile(MapServerHandle, FILE_MAP_ALL_ACCESS, 0, 0, Size);<br>end;<br><br><br>procedure CloseShareData();<br>begin<br>UnmapViewOfFile(SData);<br>CloseHandle(MapServerHandle);<br>end;<br><br>function EnableHook(h: HWND): Boolean; stdcall;<br>begin<br>if SData.hHk <> 0 then<br>begin<br> Result := false;<br> exit;<br>end;<br>SData.Wnd := h;<br>SData.UniqueMsgID := RegisterWindowMessage('RWM1');<br><br>SData.hHk := SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, hInstance, 0);<br><br>Result := SData.hHk <> 0;<br>end;<br><br>function DisableHook(): Boolean; stdcall;<br>begin<br>//注销挂钩<br>Result := false;<br>if UnhookWindowsHookEx(SData.hHk) then<br>begin<br> SData.hHk := 0;<br> Result := true;<br>end;<br>end;<br><br>procedure HookExit;<br>begin<br>if SData.hHk <> 0 then<br> DisableHook;<br>ExitProc := procExit;<br>end;<br><br><br>function GetMsgProc(nCode: integer; wParam, lParam: longint): LRESULT;<br>stdcall;<br>var<br>pInfo: TMsg;<br>begin<br>if nCode < 0 then<br>begin<br> Result := CallNextHookEx(hhk, nCode, wParam, lParam);<br> exit;<br>end;<br><br>if (nCode = HC_ACTION) and (wParam = PM_REMOVE) then<br> begin<br> pInfo := TMsg(PMsg(lparam)^);<br> if pInfo.message = WM_CLOSE then<br> begin<br> //做你想做的,不过一般是使用sendmessage来发送消息的<br> SendMessage(SData.Wnd, SData.UniqueMsgID, 0, 0); <br> end;<br> end;<br>Result := CallNextHookEx(hhk, nCode, wParam, lParam);<br>end;<br><br>=======================================================================<br>主程序:<br><br>unit Unit1;<br><br>interface<br><br>uses<br>Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>Dialogs, StdCtrls, ExtCtrls;<br><br>type<br>TForm1 = class(TForm)<br> Label1: TLabel;<br> Button1: TButton;<br> Edit1: TEdit;<br> procedure FormCreate(Sender: TObject);<br> procedure FormDestroy(Sender: TObject);<br>private<br> { Private declarations }<br> procedure WndProc(var Msg: TMessage); override;<br>public<br> { Public declarations }<br>end;<br><br>function EnableHook(h: HWND): Boolean; stdcall;<br>function DisableHook(): Boolean; stdcall;<br><br>var<br>Form1: TForm1;<br>UniqueMsgID: Cardinal;<br><br>implementation<br>function EnableHook; external 'MHook.dll' name 'EnableHook';<br>function DisableHook; external 'MHook.dll' name 'DisableHook';<br><br><br>{$R *.dfm}<br><br>procedure TForm1.WndProc(var Msg: TMessage);<br>var<br>ptMousePos: Tpoint;<br>begin<br>if Msg.msg = UniqueMsgID then<br>begin<br> GetCursorPos(ptMousePos);<br> label1.Caption := 'X:' + inttostr(ptMousePos.X) + ' Y:' + inttostr(ptMousePos.Y);<br>end;<br>inherited WndProc(Msg); ;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>h: HWND;<br>begin<br>UniqueMsgID := RegisterWindowMessage('RWM1');<br>EnableHook(Handle);<br>end;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br>DisableHook;<br>end;<br><br>end.<br>