如何通過hook,獲得輸入法輸入的文字.200分奉上!!! (200分)

  • 主题发起人 主题发起人 holyknight
  • 开始时间 开始时间
H

holyknight

Unregistered / Unconfirmed
GUEST, unregistred user!
如何通過hook,獲得輸入法輸入的文字<br>我用了如下的方法可是沒有效果,又沒有其他好的方法呢??<br><br>function IMEHookProc(iCode: integer; wParam: wParam; lParam: lParam):LResult; stdcall;<br>begin<br>...<br>&nbsp; case PMsg(LParam).message of<br>&nbsp; &nbsp; WM_IME_CHAR:<br>&nbsp; &nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ShowMessage('Process IME Hooks');//{在這裡判斷消息為WM_IME_CHAR <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 可是在執行時沒有任何效果,那位大俠<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 知道更好的解決方法嗎??}<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; end;<br>...<br>end;<br><br><br>function SetIMEHook: bool; export;<br>begin<br>...<br>hNextIMEHookProc := SetWindowsHookEx(WH_GETMESSAGE, IMEHookProc, HInstance, 0);<br>...<br>end;<br><br><br>
 
做了个例子给你:<br>DLL:<br><br>library www;<br><br>uses<br>&nbsp; sharemem,<br>&nbsp; Windows,<br>&nbsp; Messages,<br>&nbsp; SysUtils,<br>&nbsp; we in 'we.pas';<br><br>{$r *.res}<br><br>exports<br>&nbsp;run,Stop;<br>begin<br>end.<br><br><br>unit we;<br><br><br>interface<br>uses Windows,Messages, SysUtils,TLHelp32,classes,forms;<br>var<br>&nbsp; HHCallWndProc:HHook;<br>&nbsp; OldApplication : TApplication;<br>&nbsp; procedure stop;stdcall;<br>&nbsp; procedure run(AHandle:THandle);stdcall;<br><br>implementation<br><br><br>//&amp;sup1;&amp;laquo;&amp;sup1;&amp;sup2;&amp;sup1;&amp;sup3;×&amp;Oacute;&amp;ordm;&amp;macr;&amp;Ecirc;&amp;yacute;<br>procedure HookProc(OldLparam:LPARAM;uMessage:integer;wParam:WPARAM;lParam:LPARAM);stdcall;<br>var<br>&nbsp;ExtendStr:String;<br>&nbsp;SS:TSTrings;<br>begin<br>&nbsp; if (uMessage=WM_IME_CHAR) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;Application.MessageBox('sss','ss',mb_ok);<br>&nbsp; &nbsp; end;<br>end;<br><br>//&amp;acute;°&amp;iquest;&amp;Uacute;&amp;Iuml;&amp;ucirc;&amp;Iuml;&amp;cent;&amp;sup1;&amp;sup3;×&amp;Oacute;&amp;frac12;&amp;Oslash;&amp;raquo;&amp;ntilde;&amp;ordm;&amp;ordm;×&amp;Ouml;&amp;Ecirc;&amp;auml;&amp;Egrave;&amp;euml;<br>function CallWndProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall; <br>var <br>&nbsp; pcs:PCWPSTRUCT;<br>begin <br>&nbsp; pcs:=PCWPSTRUCT(lParam);<br>&nbsp; if (nCode&gt;=0) and (pcs&lt;&gt;nil) and (pcs^.hwnd&lt;&gt;0) &nbsp;then<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;if (pcs^.message=WM_IME_CHAR) then<br>&nbsp; &nbsp; &nbsp; Application.MessageBox('sss','ss',mb_ok);<br>&nbsp; end; <br>&nbsp; Result:=CallNextHookEx(HHCallWndProc,nCode,wParam,lParam); <br>end;<br>//&amp;AElig;&amp;ocirc;&amp;para;&amp;macr;&amp;Iacute;&amp;pound;&amp;Ouml;&amp;sup1;&amp;sup1;&amp;sup3;×&amp;Oacute;<br>procedure SetHook(fSet:boolean); <br>begin<br>&nbsp; if fSet=true then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; if HHCallWndProc=0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; HHCallWndProc:=SetWindowsHookEx(WH_CALLWNDPROC,@CallWndProc,hinstance,0);<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; end<br>&nbsp;else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; if HHCallWndProc&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp;UnhookWindowsHookEx(HHCallWndProc);<br>&nbsp; &nbsp;end;<br>end;<br>// &amp;Iacute;&amp;pound;&amp;Ouml;&amp;sup1;<br>procedure stop;stdcall;<br>begin<br>&nbsp; &nbsp;SetHook(False);<br>&nbsp; &nbsp;Application.Handle := OldApplication.Handle;<br>end;<br>//&amp;iquest;&amp;ordf;&amp;Ecirc;&amp;frac14;<br>procedure run(AHandle:THandle);stdcall;<br>begin<br>&nbsp; &nbsp;SetHook(true);<br>&nbsp; &nbsp;OldApplication := TApplication.Create(Application);<br>&nbsp; &nbsp;OldApplication.Handle := Application.Handle;<br>&nbsp; &nbsp;Application.Handle := AHandle;<br>end;<br><br>end.<br><br>调用dLL:<br>nit Test;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; &nbsp;TRunDll = procedure(AHandle: THandle);stdcall;<br>&nbsp; &nbsp;TStopDLL= procedure();stdcall;<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; &nbsp;CurDllHandle:THandle;<br><br>&nbsp; &nbsp; function RunDllFiles(DllName:String):Boolean;<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br>function TForm1.RunDllFiles(DllName:String):Boolean;<br>var<br>&nbsp; DllWorkPath:String;<br>&nbsp; RunDLL:TRunDll;<br>begin<br>&nbsp; result:=true;<br>&nbsp; DllWorkPath:='C:/Program Files/Borland/Delphi5/Projects/';<br>&nbsp; CurDllHandle:=LoadLibrary(pchar(DllWorkPath+DllName));<br>&nbsp; if CurDllHandle=0 then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;Application.MessageBox(pchar('&amp;Icirc;&amp;THORN;·¨×°&amp;Egrave;&amp;euml;&amp;para;&amp;macr;&amp;Igrave;&amp;not;&amp;iquest;&amp;acirc;'+DllName),'&amp;frac34;&amp;macr;&amp;cedil;&amp;aelig;',mb_ok+MB_ICONWARNING);<br>&nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp;end;<br>&nbsp; try<br>&nbsp; &nbsp; @RunDLL:=GetProcAddress(CurDllHandle,'run');<br>&nbsp; &nbsp; if @RunDLL&lt;&gt;nil then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RunDLL(Application.Handle);<br>&nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=FALSE;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;finally<br><br>&nbsp; &nbsp;end;<br>end;<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; DLLFileName:String;<br>begin<br>&nbsp; DLLFileName:='www.dll';<br>&nbsp; if RunDllFiles(DLLFileName) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Application.MessageBox('&amp;sup3;&amp;Eacute;&amp;sup1;&amp;brvbar;','&amp;Iuml;&amp;micro;&amp;Iacute;&amp;sup3;&amp;ETH;&amp;Aring;&amp;Iuml;&amp;cent;',mb_ok+mb_iconinformation);<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;Application.MessageBox('&amp;Ecirc;§°&amp;Uuml;','&amp;Iuml;&amp;micro;&amp;Iacute;&amp;sup3;&amp;ETH;&amp;Aring;&amp;Iuml;&amp;cent;',MB_ICONWARNING+mb_ok);<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; StopDLL:TStopDll;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; @StopDLL:=GetProcAddress(CurDllHandle,'stop');<br>&nbsp; &nbsp; if @StopDLL&lt;&gt;nil then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StopDLL;<br>&nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //result:=FALSE;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp;FreeLibrary(CurDllHandle);<br>&nbsp; &nbsp;end;<br>end;<br><br>你的问题是:
 
对不起:<br>procedure HookProc(OldLparam:LPARAM;uMessage:integer;wParam:WPARAM;lParam:LPARAM);stdcall;应该删掉:<br>得到汉字:<br>用:<br>if (pcs^.message=WM_IME_CHAR) then<br>&nbsp;begin<br>&nbsp; &nbsp; &nbsp;CurStr:=format('%s%s',[chr((pcs^.wParam shr 8) and $ff),chr(pcs^.wParam and $ff)]);<br>&nbsp; &nbsp; &nbsp;Application.MessageBox(pcahr(CurStr),'ss',mb_ok);<br>&nbsp;end;<br><br>
 
這個方法不適用於Microsoft Word和IE呀,請問在這兩個下面如何做呢?
 
关注 &nbsp; &nbsp; hook编程还没有接触过,呵呵呵
 
http://www.138soft.com/KeyTest.zip<br>
 
to: jingtao<br>你的GetKey.dll是不错。在进程中看不到运行程序的名字。<br>可是不知道在WIN98可不可以运行。还有在play.txt会出现乱码。<br>能不能再改进一下。我个人非常敬佩你。<br>可不可以发一些代码,让我们这些大富翁学学。<br><br>&nbsp;
 
&nbsp;word是没有WM_IME_CHAR消息的。。
 
后退
顶部