请高手给菜鸟讲解一下下面这个函数里头的东西都是什么?(41分)

  • 主题发起人 主题发起人 wanglong3
  • 开始时间 开始时间
W

wanglong3

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; EventLog:=0;<br>&nbsp; //建立键盘鼠标操作消息纪录链<br>&nbsp; hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);<br>end;<br><br>function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;<br>begin<br>&nbsp; recOK:=1;<br>&nbsp; Result:=0;<br><br>&nbsp; if iCode &lt; 0 then<br>&nbsp; &nbsp; Result := CallNextHookEx(hHook,iCode,wParam,lParam)<br>&nbsp; else if iCode = HC_SYSMODALON then<br>&nbsp; &nbsp; recOK:=0<br>&nbsp; else if iCode = HC_SYSMODALOFF then<br>&nbsp; &nbsp; recOK:=1<br>&nbsp; else if ((recOK&gt;0) and (iCode = HC_ACTION)) then begin<br>&nbsp; &nbsp; EventArr[EventLog]:=pEventMSG(lParam)^;<br>&nbsp; &nbsp; EventLog:=EventLog+1;<br><br>&nbsp; &nbsp; if EventLog&gt;=1000 then begin<br>&nbsp; &nbsp; &nbsp; UnHookWindowsHookEx(hHook);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>这里HC_SYSMODALON,HC_SYSMODALOFF,HC_GETNEXT,HC_SKIP是什么?
 
哦,对不起,还有这个函数里<br>function PlayProc(iCode: Integer; wParam: wParam; lParam: lParam): LRESULT; stdcall;<br>begin<br>&nbsp; canPlay := 1;<br>&nbsp; Result := 0;<br><br>&nbsp; if iCode &lt; 0 then //必须将消息传递到消息链的下一个接受单元<br>&nbsp; &nbsp; Result := CallNextHookEx(hPlay, iCode, wParam, lParam)<br>&nbsp; else if iCode = HC_SYSMODALON then<br>&nbsp; &nbsp; canPlay := 0<br>&nbsp; else if iCode = HC_SYSMODALOFF then<br>&nbsp; &nbsp; canPlay := 1<br>&nbsp; else &nbsp; if ((canPlay = 1) and (iCode = HC_GETNEXT)) then<br>&nbsp; begin<br>&nbsp; &nbsp; if bDelay then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; bDelay := False;<br>&nbsp; &nbsp; &nbsp; Result := 50;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; pEventMSG(lParam)^ := EventArr[PlayLog];<br>&nbsp; end<br>&nbsp; else if ((canPlay = 1) and (iCode = HC_SKIP)) then<br>&nbsp; begin<br>&nbsp; &nbsp; bDelay := True;<br>&nbsp; &nbsp; PlayLog := PlayLog + 1;<br>&nbsp; end;<br>&nbsp; &nbsp;if PlayLog&gt;=EventLog then begin<br>&nbsp; &nbsp; UNHookWindowsHookEx(hPlay);<br>&nbsp; end;<br>end;<br><br>这里的HC_GETNEXT,HC_SKIP是什么??
 
Hook之类的,我也听课……
 
我也是菜鸟,听课ing.
 
没有讲课的只有听课的,哎,自己up。<br>其实这段代码我是在这看到的<br>http://www.hktk.com/soft/program/article/delphi/delphi524.html<br>怎么大富翁里没人研究过吗?
 
是个钩子函数,可以拦截、记录系统的消息!<br>
 
我在书店里面见过这方面的书.还可以.钩子技术.....<br>但我没有研究过.
 
可能是钩子函数的一些常量。
 
一个钩子函数罢了,记录系统的指定信息的.
 
钩子函数,没用过。
 
听课中,只能帮你提前了
 
用日志钩子也可实现,最好对照上面那位仁兄的例子把下面的程序改为delphi语言。<br>(转载)<br>日志钩子(JournalRecord Hook)的使用 <br>---- 钩子是WINDOWS中消息处理机制的一个要点,通过安装各种钩子,应用程序能够设置相应的子例程来监视系统里的消息传递以及在这些消息到达目标窗口程序之前处理它们。钩子的种类很多,每种钩子可以截获并处理相应的消息,如键盘钩子可以截获键盘消息,鼠标钩子可以截获鼠标消息,外壳钩子可以截获启动和关闭应用程序的消息,日志钩子可以监视和记录输入事件。钩子分为线程专用钩子和全局钩子,线程专用钩子只监视指定的线程,要监视系统中的所有线程,必须用到全局钩子。对于全局钩子,钩子函数必须包含在独立的动态链接库(DLL)中,这样才能被各种相关联的应用程序调用。在WINDOWS中,日志钩子是个很特别的钩子,它只有全局钩子一种,是键盘鼠标等输入设备的消息在系统消息队列被取出时发生的,而且系统中只能存在一个这样的日志钩子,更重要是,它不必用在动态链接库中,这样可以省却了为安装一个全局钩子而建立一个动态链接库的麻烦。利用日志钩子,我们可以监视各种输入事件,下面的示例可以用来记录键盘的输入,当有按键发生时,自动记录按键动作的日期和时间以及当前激活的窗口名称。本示例在中文WIN98,Borland C++ Builder4中编译通过。 <br><br>---- 1.新建一个工程,在窗体Form1中放置两个按钮Button1和Button2, CAPTION分别为“安装日志钩子”和“卸载日志钩子”。 <br><br>---- 2. 定义如下全局变量: <br><br>HHOOK g_hLogHook=NULL; &nbsp; &nbsp; //钩子变量<br>HWND g_hLastFocus=NULL; &nbsp; &nbsp; <br>//记录上一次得到焦点的窗口句柄<br>const int KeyPressMask=0x80000000; &nbsp;//键盘掩码常量<br>char g_PrvChar; &nbsp; &nbsp; &nbsp;//保存上一次按键值<br><br>3.在Button1的OnClick事件中输入:<br><br>void __fastcall TForm1::Button1Click(TObject *Sender)<br>&nbsp;{<br>&nbsp; if &nbsp;(g_hLogHook==NULL)<br>&nbsp; &nbsp;g_hLogHook = SetWindowsHookEx<br>(WH_JOURNALRECORD,<br>&nbsp; &nbsp; &nbsp; &nbsp; (HOOKPROC)JournalLogProc,<br>HInstance,0); &nbsp;//安装日志钩子<br>&nbsp;}<br><br>4.在Button2的OnClick事件中输入:<br><br>void __fastcall TForm1::Button2Click(TObject *Sender)<br>{<br>&nbsp;if (g_hLogHook!=NULL)<br>&nbsp; {UnhookWindowsHookEx(g_hLogHook);<br>&nbsp; &nbsp;g_hLogHook=NULL;<br>&nbsp; } &nbsp;//卸载日志钩子<br>}<br><br>5.输入钩子回调函数:<br>HOOKPROC JournalLogProc(int iCode, <br>WPARAM wParam, LPARAM lParam)<br>{<br>&nbsp;if (iCode&lt; 0) return (HOOKPROC)CallNextHookEx<br>(g_hLogHook,iCode,wParam,lParam);<br>&nbsp;if (iCode==HC_ACTION)<br>&nbsp; {EVENTMSG *pEvt=(EVENTMSG *)lParam;<br>&nbsp; &nbsp;int i;<br>&nbsp; &nbsp;HWND hFocus; &nbsp; &nbsp; &nbsp;//保存当前活动窗口句柄<br>&nbsp; &nbsp;char szTitle[256]; &nbsp; &nbsp; //当前窗口名称<br>&nbsp; &nbsp;char szTime[128]; &nbsp; &nbsp;//保存当前的日期和时间<br>&nbsp; &nbsp;FILE *stream=fopen(“c://logfile.txt”,"a+t");<br>&nbsp; &nbsp;if (pEvt-&gt;message==WM_KEYDOWN) &nbsp; &nbsp; <br>&nbsp; &nbsp; {int vKey=LOBYTE(pEvt- &gt;paramL); &nbsp; &nbsp;// 取得虚拟键值<br>&nbsp; &nbsp; &nbsp;char ch;<br>&nbsp; &nbsp; &nbsp;char str[10];<br>&nbsp; &nbsp; &nbsp;hFocus=GetActiveWindow(); &nbsp; &nbsp; <br>&nbsp; //取得当前活动窗口句柄<br>&nbsp; &nbsp; &nbsp;if(g_hLastFocus!=hFocus) &nbsp; &nbsp; <br>&nbsp; //当前活动窗口是否改变<br>&nbsp; &nbsp; &nbsp; {GetWindowText(hFocus,szTitle,256);<br>&nbsp; &nbsp; &nbsp; &nbsp;g_hLastFocus=hFocus;<br>&nbsp; &nbsp; &nbsp; &nbsp;strcpy(szTime,DateTimeToStr(Now())<br>.c_str()); &nbsp;//得到当前的日期时间<br>&nbsp; &nbsp; &nbsp; &nbsp;fprintf(stream,"%c%s%c%c%s",<br>10,szTime,32,32,szTitle); &nbsp;//写入文件<br>&nbsp; &nbsp; &nbsp; &nbsp;fprintf(stream,"%c%c",32,32); &nbsp;<br>&nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp;int iShift=GetKeyState(0x10); &nbsp;<br>//测试SHIFT,CAPTION,NUMLOCK等键是否按下<br>&nbsp; &nbsp; &nbsp;int iCapital=GetKeyState(0x14);<br>&nbsp; &nbsp; &nbsp;int iNumLock=GetKeyState(0x90);<br>&nbsp; &nbsp; &nbsp;bool bShift=(iShift &amp; KeyPressMask)==KeyPressMask; &nbsp; <br>&nbsp; &nbsp; &nbsp;bool bCapital=(iCapital &amp; 1)==1;<br>&nbsp; &nbsp; &nbsp;bool bNumLock=(iNumLock &amp; 1)==1;<br>&nbsp; &nbsp; &nbsp;if (vKey &gt;=48 &amp;&amp; vKey&lt; =57) <br>&nbsp;// 数字0-9<br>&nbsp; &nbsp; &nbsp; &nbsp;if (!bShift) fprintf(stream,"%c",vKey);<br>&nbsp; &nbsp; &nbsp;if (vKey &gt;=65 &amp;&amp; vKey&lt; =90) <br>// A-Z &nbsp; &nbsp; &nbsp; a-z<br>&nbsp; &nbsp; &nbsp; {if (!bCapital)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (bShift) ch=vKey; else ch=vKey+32;<br>&nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (bShift) ch=vKey+32; else ch=vKey;<br>&nbsp; &nbsp; &nbsp; &nbsp;fprintf(stream,"%c",ch);<br>&nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp;if (vKey &gt;=96 &amp;&amp; vKey&lt; =105) &nbsp; &nbsp; &nbsp; &nbsp; // 小键盘0-9<br>&nbsp; &nbsp; &nbsp; &nbsp;if (bNumLock) fprintf(stream,"%c",vKey-96+48);<br>&nbsp; &nbsp; &nbsp;if (vKey&gt;=186 &amp;&amp; vKey&lt;=222) &nbsp; &nbsp; &nbsp; &nbsp; // 其他键<br>&nbsp; &nbsp; &nbsp; {switch (vKey)<br>&nbsp; &nbsp; &nbsp; &nbsp; {case 186:if (!bShift) ch=';'; else ch=':';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 187:if (!bShift) ch='='; else ch='+';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 188:if (!bShift) ch=','; else ch='&lt;' ;break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 189:if (!bShift) ch='-'; else ch='_';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 190:if (!bShift) ch='.'; else ch=' &gt;';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 191:if (!bShift) ch='/'; else ch='?';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 192:if (!bShift) ch='`'; else ch='~';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 219:if (!bShift) ch='['; else ch='{';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 220:if (!bShift) ch='//'; else ch='|';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 221:if (!bShift) ch=']'; else ch='}';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 222:if (!bShift) ch='/''; else ch='/"';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default:ch='n';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp;if (ch!='n') fprintf(stream,"%c",ch);<br>&nbsp; &nbsp; &nbsp; }<br>// &nbsp; &nbsp; if (wParam &gt;=112 &amp;&amp; wParam&lt;=123) &nbsp; &nbsp;<br>&nbsp;// 功能键 &nbsp; [F1]-[F12]<br>&nbsp; &nbsp; &nbsp;if (vKey &gt;=8 &amp;&amp; vKey&lt; =46) &nbsp; //方向键<br>&nbsp; &nbsp; &nbsp; {switch (vKey)<br>&nbsp; &nbsp; &nbsp; &nbsp; {case 8:strcpy(str,"[BK]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 9:strcpy(str,"[TAB]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 13:strcpy(str,"[EN]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 32:strcpy(str,"[SP]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 33:strcpy(str,"[PU]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 34:strcpy(str,"[PD]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 35:strcpy(str,"[END]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 36:strcpy(str,"[HOME]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 37:strcpy(str,"[LF]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 38:strcpy(str,"[UF]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 39:strcpy(str,"[RF]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 40:strcpy(str,"[DF]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 45:strcpy(str,"[INS]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case 46:strcpy(str,"[DEL]");break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default:ch='n';break;<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp;if (ch!='n')<br>&nbsp; &nbsp; &nbsp; &nbsp; {if (g_PrvChar!=vKey)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {fprintf(stream,"%s",str);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;g_PrvChar=vKey;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; }<br>}<br>&nbsp; &nbsp; &nbsp; if<br>(pEvt- &gt;message==WM_LBUTTONDOWN || pEvt- &gt;message<br>==WM_RBUTTONDOWN)<br>&nbsp; &nbsp; &nbsp; {hFocus=GetActiveWindow();<br>&nbsp; &nbsp; &nbsp; &nbsp;if (g_hLastFocus!=hFocus)<br>&nbsp; &nbsp; &nbsp; &nbsp; {g_hLastFocus=hFocus;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetWindowText(hFocus,szTitle,256); &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp;strcpy(szTime,DateTimeToStr(Now()).c_str()); &nbsp;<br>//得到当前的日期时间<br>&nbsp; &nbsp; &nbsp;fprintf(stream,"%c%s%c%c%s",<br>10,szTime,32,32,szTitle); &nbsp;//写入文件<br>&nbsp; &nbsp; &nbsp;fprintf(stream,"%c%c",32,32); &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; }<br>&nbsp;fclose(stream);<br>&nbsp;return (HOOKPROC)CallNextHookEx<br>(g_hLogHook,iCode,wParam,lParam);<br>}<br><br>---- 将工程编译执行后,每当激活一个窗口时,就会把当前窗口名称写入文件c:/logfile.txt中,当有按键时,按键的名称也会写入此文件中,这里的并没有处理全部的按键,读者可根据需要添加相应的语句。要捕捉键盘的按键动作,用键盘钩子(Keyboard Hook)也同样可以实现,但是用日志钩子却比键盘钩子要方便许多。首先,如果要捕捉其他应用程序的按键,即做成全局钩子,键盘钩子一定要单独放在动态链接库中,而日志钩子却不必;其次,在键盘钩子函数得到的键盘按键之前,系统已经处理过这些输入了,如果系统把这些按键屏蔽掉,键盘钩子就无法检测到它们,例如,当输入屏幕保护程序密码时,键盘钩子无法检测到用户输入了那些字符,而日志钩子却可以检测到。 <br><br>
 
钩子函数,对此没有研究
 
今天看了一会帮助,不知道这么理解对否?<br>else if iCode = HC_SYSMODALON then<br>&nbsp; &nbsp;recOK:=0<br>&nbsp;else if iCode = HC_SYSMODALOFF then<br>&nbsp; &nbsp;recOK:=1<br>&nbsp;else if ((recOK&gt;0) and (iCode = HC_ACTION)<br>“HC_SYSMODALON”, “HC_SYSMODALOFF ”类似于调用了showmodal,如果在modal状态下,也就没有必要记录操作了,所以 recOK:=0;否则 recOK:=1;<br>然后 如果要记录而且是活动的窗口,则记录操作。<br>这是Windows.pas文件中的定义:<br>&nbsp; { Hook Codes }<br>&nbsp; {$EXTERNALSYM HC_ACTION}<br>&nbsp; HC_ACTION = 0;<br>&nbsp; {$EXTERNALSYM HC_GETNEXT}<br>&nbsp; HC_GETNEXT = 1;<br>&nbsp; {$EXTERNALSYM HC_SKIP}<br>&nbsp; HC_SKIP = 2;<br>&nbsp; {$EXTERNALSYM HC_NOREMOVE}<br>&nbsp; HC_NOREMOVE = 3;<br>&nbsp; {$EXTERNALSYM HC_NOREM}<br>&nbsp; HC_NOREM = HC_NOREMOVE;<br>&nbsp; {$EXTERNALSYM HC_SYSMODALON}<br>&nbsp; HC_SYSMODALON = 4;<br>&nbsp; {$EXTERNALSYM HC_SYSMODALOFF}<br>&nbsp; HC_SYSMODALOFF = 5;<br><br>
 
多人接受答案了。
 
后退
顶部