哪儿有日志hook可以下载?(101分)

  • 主题发起人 主题发起人 file1
  • 开始时间 开始时间
F

file1

Unregistered / Unconfirmed
GUEST, unregistred user!
最近想写一个监视键盘按键情况(比如在word里写“HyR”,我就希望直接得到它,类似于key_press事件。<br>而不是记录“shift+h+y+shift+r”这样的东西)的程序,应该是要用到hook函数。<br>但我找到的全部都是关于监控键盘哪些键被按下的hook程序(就是“shift+h+y+shift+r”的东西),<br>——通过keboardproc来的。难道说我需要的程序就是在这种程序(监控键盘哪些键被按下的hook程序)<br>下完善过滤后得到的吗?还是有另外的的方法实现的?<br>看到这儿有人曾经提到的日志hook好像就是我需要的那种hook程序,好像它还能监控汉字输入,<br>那么应该是用别的方法实现的了。http://211.101.4.25/delphibbs/dispq.asp?lid=582145<br>实在很期待高人给予指点!!和告知我哪儿有日志hook的下载!
 
WIN95中日志钩子(JournalRecord Hook)的使用 <br>东莞生益敷铜板股份有限公司 <br>肖粤斌 <br>---- 钩子是WINDOWS中消息处理机制的一个要点,通过安装各种钩子,应用程序能够设置<br>应的子例程来监视系统里的消息传递以及在这些消息到达目标窗口程序之前处理它们。<br>钩子的种类很多,每种钩子可以截获并处理相应的消息,如键盘钩子可以截获键盘消息<br>,鼠标钩子可以截获鼠标消息,外壳钩子可以截获启动和关闭应用程序的消息,日志钩子<br>可以监视和记录输入事件。钩子分为线程专用钩子和全局钩子,线程专用钩子只监视指定<br>的线程,要监视系统中的所有线程,必须用到全局钩子。对于全局钩子,钩子函数必须包含<br>在独立的动态链接库(DLL)中,这样才能被各种相关联的应用程序调用。在WINDOWS中,<br>日志钩子是个很特别的钩子,它只有全局钩子一种,是键盘鼠标等输入设备的消息在<br>系统消息队列被取出时发生的,而且系统中只能存在一个这样的日志钩子,更重要是,<br>它不必用在动态链接库中,这样可以省却了为安装一个全局钩子而建立一个动态链接库<br>的麻烦。利用日志钩子,我们可以监视各种输入事件,下面的示例可以用来记录键盘的输入,<br>当有按键发生时,自动记录按键动作的日期和时间以及当前激活的窗口名称。<br>本示例在中文WIN98,Borland C++ Builder4中编译通过。 <br>---- 1.新建一个工程,在窗体Form1中放置两个按钮Button1和Button2, CAPTION分别为<br>“安装日志钩子”和“卸载日志钩子”。 <br>---- 2. 定义如下全局变量: <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>3.在Button1的OnClick事件中输入:<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中,<br>当有按键时,按键的名称也会写入此文件中,这里的并没有处理全部的按键,读者可根据<br>需要添加相应的语句。要捕捉键盘的按键动作,用键盘钩子(Keyboard Hook)也同样可以实现<br>,但是用日志钩子却比键盘钩子要方便许多。首先,如果要捕捉其他应用程序的按键,<br>即做成全局钩子,键盘钩子一定要单独放在动态链接库中,而日志钩子却不必;<br>其次,在键盘钩子函数得到的键盘按键之前,系统已经处理过这些输入了,如果系统把这些按键<br>屏蔽掉,键盘钩子就无法检测到它们,例如,当输入屏幕保护程序密码时,键盘钩子无法<br>检测到用户输入了那些字符,而日志钩子却可以检测到。 <br>---- 无论是哪种钩子, 都会增加系统处理消息的时间,从而降低系统的性能,我们只有在<br>必要的时候才安装这些钩子,而且尽可能在不需要时移走它们。 <br>中国计算机世界出版服务公司版权所有 <br>
 
接受答案了.
 
后退
顶部