>>HOOK我已经做好了<br>你可以将钩子编译为DLL~<br><br>下面是一个取词的源码:--转载 [
][
]<br>这是dll文件:<br>library dll_HookMouse;<br>uses<br> SysUtils,<br> Windows,<br> Classes,<br> Messages,<br> Math,<br> Dialogs,<br> U_Def in 'U_Def.pas';<br>{$R *.RES}<br>var<br> hMouseHook : HHOOK;<br> SpyInstalled : Boolean;<br> fTimerID : Cardinal;<br> pShMem : PShareMem;<br> hMappingFile : THandle;<br>function InstallSpy:Boolean; forward;<br>function UnWiseSpy:Boolean; forward;<br>function fExtTextOutA(theDC :HDC; nXStart, nYStart :integer; toOptions : Lon<br>gint; rect : PRect;<br> lpStr
AnsiChar; nCount :Longint; Dx: PInteger):BOOL;<br> stdcall;<br>var<br> dwBytes, dwCallingProc : DWORD;<br> pOldExtTextOut : _ExtTextOutA;<br> hModuleGDI : THandle;<br> poOri, poDC, poText, poMouse : TPoint;<br> Size : TSize;<br>begin<br> UnWiseSpy;<br> GetWindowThreadProcessID(pShMem^.hHookWnd, @dwCallingProc);<br> try<br> if pShMem^.bCanSpyNow and (dwCallingProc <> pShMem^.hProc) then<br> begin<br> dwBytes := Min(nCount, MaxStringLen);<br> CopyMemory(@(pShMem^.fStrExtTextOutA), lpStr, dwBytes);<br> //Get lpStr Content<br> //The following codes for get the right text<br> GetDCOrgEx(theDC, poOri);<br> // 取得本窗口设备相关坐标原点的全局逻辑坐标<br> poDC.x := nXStart;<br> poDC.y := nYStart;<br> //<br> LPToDP(theDC, poDC, 1);<br> //全局逻辑坐标转化为设备相关坐标<br> GetCursorPos(poMouse);<br> poText.x := poDC.x + poOri.x;<br> poText.y := poDC.y + poOri.y;<br> if (GetTextAlign(theDC) and TA_UPDATECP) <> 0 then<br> begin<br> GetCurrentPositionEx(theDC, @poOri);<br> poText.x := poText.x + poOri.x;<br> poText.y := poText.y + poOri.y;<br> end;<br> GetTextExtentPoint(theDC, lpStr, nCount, Size);<br> // 取得要输出的字符串的实际显示大小<br> if (poMouse.x >= poText.x) and (poMouse.x <= poText.x + Size.cx) and<br> (poMouse.y >= poText.y) and (poMouse.y <= poText.y + Size.cy) then<br> begin<br> pShMem^.bCanSpyNow := False;<br> pShMem^.nTimePassed := -1;<br> end;<br> pShMem^.fStrExtTextOutA[dwBytes] := Chr(0);<br> FlushViewOfFile(pShMem, 0);<br> if dwCallingProc <> pShMem^.hProc then<br> PostMessage(pShMem^.hProcWnd, WM_MOUSEPT, 2, 2);<br> end;<br> if (dwCallingProc = pShMem^.hProc) or pShMem^.bHookExtTextOutA then<br> begin<br> hModuleGDI := GetModuleHandle(PChar('GDI32'));<br> @pOldExtTextOut := GetProcAddress(hModuleGDI, PChar('ExtTextOutA'));<br> Result := pOldExtTextOut(theDC, nXStart, nYStart, toOptions, rect, lpS<br>tr, nCount, Dx);<br> end else<br> Result := True;<br> except<br> Result := False;<br> end;<br> SpyInstalled := True;<br> InstallSpy;<br>end;<br>function UnWiseSpy:Boolean;<br>var<br> dwBytesWritten, dwOldProtect : DWORD;<br> pOldExtTextOut : _ExtTextOutA;<br> hModuleGDI : THandle;<br>begin<br> hModuleGDI := GetModuleHandle(PChar('GDI32'));<br> @pOldExtTextOut := GetProcAddress(hModuleGDI, PChar('ExtTextOutA'));<br> if not VirtualProtect(@pOldExtTextOut, SizeOf(TLongJump), PAGE_EXECUTE_REA<br>DWRITE, @dwOldProtect) then<br> begin<br> Result := False;<br> Exit;<br> end;<br> if not WriteProcessMemory(GetCurrentProcess, @pOldExtTextOut, @pShMem^.pOl<br>dExtTextOutA, SizeOf(TLongJump), dwBytesWritten) then<br> begin<br> Result := False;<br> Exit;<br> end;<br> if not VirtualProtect(@pOldExtTextOut, SizeOf(TLongJump), dwOldProtect, @d<br>wBytesWritten) then<br> begin<br> Result := False;<br> Exit;<br> end;<br> Result := True;<br>end;<br>function InstallSpy:Boolean;<br>var<br> dwBytesWritten, dwOldProtect : DWORD;<br> ljHere : TLongJump;<br> pOldExtTextOut : _ExtTextOutA;<br> hModuleGDI : THandle;<br>begin<br> hModuleGDI := GetModuleHandle(PChar('GDI32'));<br> @pOldExtTextOut := GetProcAddress(hModuleGDI, PChar('ExtTextOutA'));<br> if not VirtualProtect(@pOldExtTextOut, SizeOf(TLongJump), PAGE_EXECUTE_REA<br>DWRITE, @dwOldProtect) then<br> begin<br> Result := False;<br> Exit;<br> end;<br> ljHere.JmpOp := CodeJump;<br> ljHere.Addr := Pointer( Cardinal(@fExtTextOutA) - Cardinal(@pOldExtTextOut<br>) - SizeOf(TLongJump) );<br> if not WriteProcessMemory(GetCurrentProcess, @pOldExtTextOut, @ljHere, Siz<br>eOf(TLongJump), dwBytesWritten) then<br> begin<br> Result := False;<br> Exit;<br> end;<br> if not VirtualProtect(@pOldExtTextOut, SizeOf(TLongJump), dwOldProtect, @d<br>wBytesWritten) then<br> begin<br> Result := False;<br> Exit;<br> end;<br> Result := True;<br>end;<br>function MouseHookProc(nCode : integer; wPar : WParam; lPar : LParam) : lRes<br>ult; stdcall;<br>var<br> pMouseInf : TMouseHookStruct;<br>begin<br> if (not SpyInstalled) and pShMem^.bHookExtTextOutA then<br> InstallSpy;<br> if SpyInstalled and (not pShMem^.bHookExtTextOutA) then<br> begin<br> UnwiseSpy;<br> SpyInstalled := False;<br> end;<br> pShMem^.nTimePassed := 0 ;<br> if (nCode >= 0) and (wPar = WM_MOUSEMOVE) then<br> begin<br> pMouseInf := (PMouseHookStruct(lPar))^;<br> if (pShMem^.pMouse.x <> pMouseInf.pt.x) or<br> (pShMem^.pMouse.y <> pMouseInf.pt.y) then<br> begin<br> if nCode = HC_NOREMOVE then<br> pShMem^.fStrMouseQueue := 'Not removed from the queue'<br> else //then HC_ACTION<br> pShMem^.fStrMouseQueue := 'Removed from the queue';<br> pShMem^.pMouse := pMouseInf.pt;<br> pShMem^.hHookWnd := pMouseInf.hwnd;<br> PostMessage(pShMem^.hProcWnd, WM_MOUSEPT, 1, 1); //1 indicates mouse m<br>essage<br> end;<br> end;<br> FlushViewOfFile(pShMem, 0);<br> Result := CallNextHookEx(hMouseHook, nCode, wPar, lPar);<br>end;<br>procedure fOnTimer(theWnd : HWND; msg, idTimer : Cardinal; dwTime : DWORD);f<br>ar pascal; //CallBack Type<br>begin<br> if pShMem^.nTimePassed = -1 then<br> Exit;<br> pShMem^.nTimePassed := pShMem^.nTimePassed + 1;<br> if pShMem^.nTimePassed > 21 then<br> begin<br> pShMem^.nTimePassed := 21;<br> FlushViewOfFile(pShMem, 0);<br> Exit;<br> end;<br> if pShMem^.nTimePassed > 20 then<br> begin<br> pShMem^.bCanSpyNow := True;<br> FlushViewOfFile(pShMem, 0);<br> SetWindowPos(pShMem^.hWndPseudo, HWND_TOPMOST, pShMem^.pMouse.x, pShMem^<br>.pMouse.y, 1, 8, SWP_NOACTIVATE or SWP_SHOWWINDOW);<br> ShowWindow(pShMem^.hWndPseudo , SW_HIDE);<br> end;<br>end;<br>function MouseWndProc(theWnd : HWND; theMess : Cardinal; wPar : wParam; lPar<br> : lParam): LResult;stdcall;<br>begin<br> case theMess of<br> WM_CLOSE :<br> begin<br> DestroyWindow(theWnd);<br> PostQuitMessage(0);<br> end;<br> else<br> begin<br> Result := DefWindowProc(theWnd, theMess, wPar, lPar);<br> Exit;<br> end;<br> end;<br> Result := 0;<br>end;<br>function InstallMouseHook(hInst : LongWord):Boolean;<br>begin<br> hMouseHook := SetWindowsHookEx(WH_MOUSE,<br> MouseHookProc,<br> GetModuleHandle(PChar('dll_HookMouse')),<br> 0);<br> if hMouseHook = 0 then<br> begin<br> Result := False;<br> Exit;<br> end;<br> pShMem^.hWndPseudo := CreateWindowEx(WS_EX_TOPMOST or WS_EX_TOOLWINDOW,<br> 'ZL_MOUSE_WND_PSEUDO',<br> 'ZL_MOUSE_WND_PSEUDO',<br> WS_CLIPSIBLINGS or WS_POPUP ,<br> 0, 0, 1, 8,<br> 0, 0,<br> hInst,<br> nil);<br> ShowWindow(pShMem^.hWndPseudo, SW_HIDE);<br> UpdateWindow(pShMem^.hWndPseudo);<br> fTimerID := SetTimer(0, 0, 10, @fOnTimer);<br> FlushViewOfFile(pShMem, 0);<br> Result := True;<br>end;<br>function UnWiseMouseHook:Boolean;<br>begin<br> KillTimer(0, fTimerID);<br> DestroyWindow(pShMem^.hWndPseudo);<br> if SpyInstalled then<br> UnWiseSpy;<br> pShMem^.bHookExtTextOutA := False;<br> FlushViewOfFile(pShMem, 0);<br> Result := UnHookWindowsHookEx(hMouseHook);<br>end;<br>procedure DllEntry(nReason : integer);<br>begin<br> case nReason Of<br> DLL_PROCESS_ATTACH:<br> begin<br> hMappingFile := CreateFileMapping($FFFFFFFF,<br> nil,<br> PAGE_READWRITE,<br> 0,<br> SizeOf(TShareMem),<br> PChar(MappingFileName));<br> if hMappingFile<>0 then //if h..=0 , the work is done by OS<br> begin<br> pShMem := PShareMem( MapViewOfFile(hMappingFile,<br> FILE_MAP_WRITE,<br> 0, //hi_order offset where mapp<br>ing begins<br> 0, //lo_order offset where mapp<br>ing begins<br> 0) ); //Size of the mapping<br> if pShMem = nil then<br> begin<br> CloseHandle(hMappingFile);<br> ShowMessage('Cannot create the Share Memory Block!');<br> end;<br> end else<br> ShowMessage('Cannot create the Share Memory Block!');<br> end;<br> DLL_PROCESS_DETACH:<br> begin<br> UnwiseSpy;<br> UnMapViewOfFile(pShMem);<br> CloseHandle(hMappingFile);<br> end;<br> else;<br> end;<br>end;<br>exports<br> MouseWndProc,<br> InstallMouseHook,<br> UnWiseMouseHook;<br>begin<br> DllProc := @DllEntry;<br> DllEntry(DLL_PROCESS_ATTACH);<br>end.<br>-------------- ----------------<br>下面是dll的接口文件:<br>unit U_Def;<br>interface<br>uses<br> Messages, Windows;<br>const<br> WM_MOUSEPT = WM_USER + 1000 + Ord('M') + Ord('P') + Ord('T');<br> MappingFileName = 'Mapping File By Raphael';<br> MaxStringLen = 50;<br> CodeJump = $E9909090;<br>type<br> PInt = ^integer;<br> _ExtTextOutA = function (theDC :HDC; nXStart, nYStart :integer; toOptions <br>: Longint; rect : PRect;<br> lpStr
AnsiChar; nCount :integer; Dx : PInteger)<br>:BOOL; stdcall;<br> _PExtTextOutA = ^_ExtTextOutA;<br> TLongJump = packed record<br> JmpOp : Cardinal;<br> Addr : Pointer;<br> end;<br> TShareMem = packed record<br> hProcWnd : HWND; //The main window of the program<br> hHookWnd : HWND; //The window currently being hooked<br> hWndPseudo : HWND; //The pseudo window used to repaint the other<br> window<br> hProc : THandle; //The process ID of the main program<br> pMouse : TPoint; //the mouse position<br> fStrMouseQueue : array [0..MaxStringLen] of Char; //mouse info<br> nTimePassed : integer; //the time passed since last time's mousemove<br><br> bCanSpyNow : Boolean;<br> bHookExtTextOutA : Boolean;<br> pOldExtTextOutA : TLongJump;<br> fStrExtTextOutA : array [0..MaxStringLen] of Char;<br> end;<br> PShareMem = ^TShareMem;<br>implementation<br>end.<br>--------------------<br>下面是主窗体文件:<br>unit U_MouseHook;<br>interface<br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls, U_Def;<br>type<br> TF_MouseHook = class(TForm)<br> Label1: TLabel;<br> e_MouseInfo: TEdit;<br> btn_HookMouse: TButton;<br> Label2: TLabel;<br> e_ExtTextOutA: TEdit;<br> btn_HookExtTextOutA: TButton;<br> procedure btn_HookMouseClick(Sender: TObject);<br> procedure FormCreate(Sender: TObject);<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> procedure btn_HookExtTextOutAClick(Sender: TObject);<br> private<br> fWndClosed, fbMouseHookInstalled : Boolean;<br> hMapObj : THandle;<br> pShMem : PShareMem;<br> procedure getMouseInfo(var theMess:TMessage); message WM_MOUSEPT;<br> public<br> { Public declarations }<br> end;<br>function InstallMouseHook(hInst : LongWord) : Boolean; external 'dll_HookMouse.dll';<br>function UnWiseMouseHook : Boolean; external 'dll_HookMouse.dll';<br>function MouseWndProc(theWnd : HWND; theMess : Cardinal; wPar : wParam; lPar : lParam): LResult; stdcall; external 'dll_HookMouse.dll';<br>var<br> F_MouseHook: TF_MouseHook;<br>implementation<br>{$R *.DFM}<br>procedure TF_MouseHook.btn_HookMouseClick(Sender: TObject);<br>begin<br> if not fbMouseHookInstalled then<br> begin<br> fbMouseHookInstalled := InstallMouseHook(hInstance);<br> if fbMouseHookInstalled then<br> begin<br> btn_HookMouse.Caption := 'Stop!';<br> btn_HookExtTextOutA.Enabled := True;<br> end else<br> ShowMessage('Cannot hook mouse!');<br> end else<br> begin<br> fbMouseHookInstalled := not UnWiseMouseHook;<br> if not fbMouseHookInstalled then<br> begin<br> btn_HookMouse.Caption := 'Hook Mouse';<br> btn_HookExtTextOutA.Enabled := False;<br> btn_HookExtTextOutA.Caption := 'Hook ExtTextOutA';<br> pShMem^.bHookExtTextOutA := False;<br> FlushViewOfFile(pShMem, 0);<br> end else<br> ShowMessage('Cannot unhook mouse!');<br> end;<br>end;<br>procedure TF_MouseHook.getMouseInfo(var theMess : TMessage);<br>begin<br> if fWndClosed then<br> Exit;<br> if theMess.LParam = 1 then //Get the Mouse info to display<br> e_MouseInfo.Text := 'X:' + IntToStr(pShMem^.pMouse.x) + ' ' +<br> 'Y:' + IntToStr(pShMem^.pMouse.y) + ' ' +<br> 'HWND:0x' + IntToHex(pShMem^.hHookWnd, 8) + ' ' +<br> pShMem^.fStrMouseQueue<br> else if theMess.LParam = 2 then //Get the ExtTextOutA display<br> e_ExtTextOutA.Text := pShMem^.fStrExtTextOutA;<br>end;<br><br><br>procedure TF_MouseHook.FormCreate(Sender: TObject);<br>var<br> hModuleGDI : THandle;<br> wc : TWndClass;<br>begin<br> hMapObj := OpenFileMapping(FILE_MAP_WRITE, //Get full access of the mapping file<br> False, //Not inheritable<br> LPCTSTR(MappingFileName)); //Name of the mapping file<br> if hMapObj = 0 then<br> begin<br> ShowMessage('Cannot locate the Share Memory Block!');<br> Halt;<br> end;<br> pShMem := PShareMem( MapViewOfFile(hMapObj,<br> FILE_MAP_WRITE,<br> 0, //hi_order offset where mapping begins<br> 0, //lo_order offset where mapping begins<br> 0) ); //Size of the mapping<br> if pShMem = nil then<br> begin<br> ShowMessage('Map File Mapping Failed! Error '+ IntToStr(GetLastError));<br> CloseHandle(hMapObj);<br> Halt;<br> end;<br> FillChar(pShMem^, SizeOf(TShareMem), 0);<br> hModuleGDI := GetModuleHandle(PChar('GDI32'));<br> if hModuleGDI = 0 then<br> begin<br> ShowMessage('Cannot get module GDI32! Error ' + IntToStr(GetLastError));<br><br> UnmapViewOfFile(pShMem);<br> CloseHandle(hMapObj);<br> Halt;<br> end;<br> CopyMemory(@pShMem^.pOldExtTextOutA, GetProcAddress(hModuleGDI, PChar('ExtTextOutA')), SizeOf(TLongJump));<br> pShMem^.hProcWnd := Self.Handle;<br> GetWindowThreadProcessID(Self.Handle, @pShMem^.hProc);<br> pShMem^.bHookExtTextOutA := False;<br> pShMem^.bCanSpyNow := False;<br> fbMouseHookInstalled := False;<br> FlushViewOfFile(pShMem, 0);<br> wc.style := 0;<br> wc.lpfnWndProc := TFNWndProc(@MouseWndProc);<br> wc.cbClsExtra := 0;<br> wc.cbWndExtra := 0;<br> wc.hInstance := HInstance;<br> wc.hIcon := 0 ;<br> wc.hCursor := 0 ;<br> wc.hbrBackground := 0 ;<br> wc.lpszMenuName := nil;<br> wc.lpszClassName := 'ZL_MOUSE_WND_PSEUDO';<br> // register the class for the main window<br> Windows.RegisterClass(wc);<br> fWndClosed := False;<br>end;<br><br><br>procedure TF_MouseHook.FormClose(Sender: TObject;<br> var Action: TCloseAction);<br>begin<br> if fbMouseHookInstalled then<br> UnWiseMouseHook;<br> UnMapViewOfFile(pShMem);<br> CloseHandle(hMapObj);<br> Windows.UnRegisterClass('ZL_MOUSE_WND_PSEUDO', hInstance);<br> fWndClosed := True;<br>end;<br>procedure TF_MouseHook.btn_HookExtTextOutAClick(Sender: TObject);<br>begin<br> if pShMem^.bHookExtTextOutA then<br> btn_HookExtTextOutA.Caption := 'Hook ExtTextOutA'<br> else<br> btn_HookExtTextOutA.Caption := 'Stop!';<br> pShMem^.bHookExtTextOutA := not pShMem^.bHookExtTextOutA;<br> FlushViewOfFile(pShMem, 0);<br>end;<br>//RaiseLastWin32Error can be used to create a GetLastError<br>//Message<br>end.<br>---------------------------<br>我做的时候分析了一下,主要用了ExtTextOutA这个API。 <br>可以取词!