怎样才能获知是哪个程序正使用钩子?(我动了所有积分!!!) ( 积分: 300 )

  • 主题发起人 主题发起人 for33
  • 开始时间 开始时间
安装DEBUG钩子,钩子回调函数中,wParam代表被截获即将调用的钩子的类型,lParam为指向DEBUGHOOKINFO结构的指针。<br>typedef struct tagDEBUGHOOKINFO { // dh &nbsp;<br> &nbsp; &nbsp;DWORD &nbsp;idThread; <br> &nbsp; &nbsp;DWORD &nbsp;idThreadInstaller; <br> &nbsp; &nbsp;LPARAM lParam; <br> &nbsp; &nbsp;WPARAM wParam; <br> &nbsp; &nbsp;int &nbsp; &nbsp;code; <br>} DEBUGHOOKINFO; <br>
 
观注,想了解![:)]
 
可不可以详细点<br>比如举个例急需
 
var hkDebug: HHook;<br>安装:hkDebug := SetWindowsHookEx(WH_DEBUG, @DebugCallback, HInstance, 0);<br>卸载:UnhookWindowsHookEx(hkDebug);<br>回调函数:<br>function DebugCallback(Code: UINT; WParam: WPARAM; LParam: LParam): LResult; stdcall;<br>var<br> &nbsp;P: PDebugHookInfo;<br>begin<br> &nbsp;if Code = HC_ACTION then<br> &nbsp;begin<br> &nbsp; &nbsp;P := PDebugHookInfo(LParam);<br> &nbsp; &nbsp;// WParam: 可以为WH_CALLWNDPROC, WH_CALLWNDPROCRET, WH_CBT, WH_DEBUG,<br> &nbsp; &nbsp;// &nbsp; &nbsp; &nbsp; &nbsp; WH_GETMESSAGE, WH_JOURNALPLAYBACK, WH_JOURNALRECORD, WH_KEYBOARD, <br> &nbsp; &nbsp;// &nbsp; &nbsp; &nbsp; &nbsp; WH_MOUSE, WH_MSGFILTER, WH_SHELL, WH_SYSMSGFILTER<br> &nbsp; &nbsp;// P.idThreadInstaller为安装钩子的线程ID,通过Toolhelp函数对系统线程进行枚举获得线程THREADENTRY32信息,<br> &nbsp; &nbsp;// 得到线程所属进程ID,再来个进程枚举,就知道是那个程序啦。<br> &nbsp;end;<br> &nbsp;Result := CallNextHookEx(hkDebug, Code, WParam, LParam);<br>end;<br>注意:上面的钩子代码要放在DLL中。
 
可不可再详细点我是新手谢谢
 
// P.idThreadInstaller为安装钩子的线程ID,通过Toolhelp函数对系统线程进行枚举获得线程THREADENTRY32信息,<br> &nbsp; &nbsp;// 得到 &nbsp; &nbsp; &nbsp;...线程所属进程ID,再来个进程枚举 &nbsp;.. &nbsp;,就知道是那个程序啦。<br>////////////<br>怎么进程枚举
 
大家不要白进来了说几名
 
2000以后无效.idThreadInstaller恒为0.只有从进程链表下手了.
 
学习高手
 
最近正在研究不完整的Win2K源代码。<br>/***************************************************************************/<br>* xxxCallHook2<br>*<br>* When you have an actual HOOK structure to call, you'd use this function.<br>* It will check to see if the hook hasn't already been unhooked, and if<br>* is it will free it and keep looking until it finds a hook it can call<br>* or hits the end of the list. &nbsp;We also make sure any needed DLLs are loaded<br>* here. &nbsp;We also check to see if the HOOK was unhooked inside the call<br>* after we return.<br>*<br>* Note: Hooking server-side window procedures (such as the desktop and console<br>* windows) can only be done by sending the hook message to the hooking app.<br>* (This is because we must not load the hookproc DLL into the server process).<br>* The hook types this can be done with are currently WH_JOURNALRECORD,<br>* WH_JOURNALPLAYBACK, WH_KEYBOARD and WH_MOUSE : these are all marked as<br>* HKF_INTERSENDABLE. &nbsp;In order to prevent a global hooker from locking up the whole<br>* system, the hook message is sent with a timeout. &nbsp;To ensure minimal<br>* performance degradation, the hooker process is set to foreground priority,<br>* and prevented from being set back to background priority with the<br>* TIF_GLOBALHOOKER bit in hooking thread's pti-&amp;gt;flags.<br>* Hooking emulated DOS apps is prevented with the TIF_DOSEMULATOR bit in the<br>* console thread: this is because these apps typically hog the CPU so much that<br>* the hooking app does not respond rapidly enough to the hook messsages sent<br>* to it. &nbsp;IanJa Nov 1994.<br>*<br>* History:<br>* 02-07-91 &nbsp; &nbsp; DavidPe &nbsp; &nbsp; Created.<br>* 1994 Nov 02 &nbsp;IanJa &nbsp; &nbsp; &nbsp; Hooking desktop and console windows.<br>/***************************************************************************/<br><br>LRESULT xxxCallHook2(<br> &nbsp; &nbsp;PHOOK phkCall,<br> &nbsp; &nbsp;int nCode,<br> &nbsp; &nbsp;WPARAM wParam,<br> &nbsp; &nbsp;LPARAM lParam,<br> &nbsp; &nbsp;LPBOOL lpbAnsiHook)<br>{<br> &nbsp; &nbsp;UINT &nbsp; &nbsp; &nbsp; &nbsp;iHook;<br> &nbsp; &nbsp;PHOOK &nbsp; &nbsp; &nbsp; phkSave;<br> &nbsp; &nbsp;LONG_PTR &nbsp; &nbsp; nRet;<br> &nbsp; &nbsp;PTHREADINFO ptiCurrent;<br> &nbsp; &nbsp;BOOL &nbsp; &nbsp; &nbsp; &nbsp;fLoadSuccess;<br> &nbsp; &nbsp;TL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tlphkCall;<br> &nbsp; &nbsp;TL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tlphkSave;<br> &nbsp; &nbsp;BYTE &nbsp; &nbsp; &nbsp; &nbsp;bHookFlags;<br> &nbsp; &nbsp;BOOL &nbsp; &nbsp; &nbsp; &nbsp;fMustIntersend;<br><br> &nbsp; &nbsp;CheckCritIn();<br><br> &nbsp; &nbsp;if (phkCall == NULL) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return 0;<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;iHook = phkCall-&amp;gt;iHook;<br><br> &nbsp; &nbsp;ptiCurrent = PtiCurrent();<br> &nbsp; &nbsp;/*<br> &nbsp; &nbsp; * Only low level hooks are allowed in the RIT context<br> &nbsp; &nbsp; * (This check used to be done in PhkFirstValid).<br> &nbsp; &nbsp; */<br> &nbsp; &nbsp;if (ptiCurrent == gptiRit) {<br> &nbsp; &nbsp; &nbsp; &nbsp;switch (iHook) {<br> &nbsp; &nbsp; &nbsp; &nbsp;case WH_MOUSE_LL:<br> &nbsp; &nbsp; &nbsp; &nbsp;case WH_KEYBOARD_LL:<br><br>#ifdef REDIRECTION<br> &nbsp; &nbsp; &nbsp; &nbsp;case WH_HITTEST:<br>#endif // REDIRECTION<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br><br> &nbsp; &nbsp; &nbsp; &nbsp;default:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return 0;<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;/*<br> &nbsp; &nbsp; * If this queue is in cleanup, exit: it has no business calling back<br> &nbsp; &nbsp; * a hook proc. Also check if hooks are disabled for the thread.<br> &nbsp; &nbsp; */<br> &nbsp; &nbsp;if ( &nbsp; &nbsp;ptiCurrent-&amp;gt;TIF_flags &amp; (TIF_INCLEANUP | TIF_DISABLEHOOKS) ||<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;((ptiCurrent-&amp;gt;rpdesk == NULL) &amp;&amp; (phkCall-&amp;gt;iHook != WH_MOUSE_LL))) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return ampiHookError[iHook + 1];<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp;/*<br> &nbsp; &nbsp; * Try to call each hook in the list until one is successful or<br> &nbsp; &nbsp; * we reach the end of the list.<br> &nbsp; &nbsp; */<br> &nbsp; &nbsp;do {<br> &nbsp; &nbsp; &nbsp; &nbsp;*lpbAnsiHook = phkCall-&amp;gt;flags &amp; HF_ANSI;<br> &nbsp; &nbsp; &nbsp; &nbsp;bHookFlags = abHookFlags[phkCall-&amp;gt;iHook + 1];<br><br> &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; * Some WH_SHELL hook types can be called from console<br> &nbsp; &nbsp; &nbsp; &nbsp; * HSHELL_APPCOMMAND added for bug 346575 DefWindowProc invokes a shell hook<br> &nbsp; &nbsp; &nbsp; &nbsp; * for console windows if they don't handle the wm_appcommand message - we need the hook<br> &nbsp; &nbsp; &nbsp; &nbsp; * to go through for csrss.<br> &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp;if ((phkCall-&amp;gt;iHook == WH_SHELL) &amp;&amp; (ptiCurrent-&amp;gt;TIF_flags &amp; TIF_CSRSSTHREAD)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((nCode == HSHELL_LANGUAGE) || (nCode == HSHELL_WINDOWACTIVATED) ||<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(nCode == HSHELL_APPCOMMAND)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bHookFlags |= HKF_INTERSENDABLE;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; &nbsp;if ((phkCall-&amp;gt;iHook == WH_SHELL) &amp;&amp; (ptiCurrent-&amp;gt;TIF_flags &amp; TIF_SYSTEMTHREAD)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((nCode == HSHELL_ACCESSIBILITYSTATE) ) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bHookFlags |= HKF_INTERSENDABLE;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; &nbsp;fMustIntersend =<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(GETPTI(phkCall) != ptiCurrent) &amp;&amp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * We always want to intersend journal hooks.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * CONSIDER (adams): Why? There's a performance hit by<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * doing so, so if we haven't a reason, we shouldn't<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * do it.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * we also need to intersend low level hooks. They can be called<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * from the desktop thread, the raw input thread AND also from<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * any thread that calls CallNextHookEx.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(bHookFlags &amp; (HKF_JOURNAL | HKF_LOWLEVEL))<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * We must intersend if a 16bit app hooks a 32bit app<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * because we can't load a 16bit dll into a 32bit process.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * We must also intersend if a 16bit app hooks another 16bit app<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * in a different VDM, because we can't load a 16bit dll from<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * one VDM into a 16bit app in another VDM (because that<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * VDM is actually a 32bit process).<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;||<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;( &nbsp; GETPTI(phkCall)-&amp;gt;TIF_flags &amp; TIF_16BIT &amp;&amp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;( &nbsp; !(ptiCurrent-&amp;gt;TIF_flags &amp; TIF_16BIT) ||<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ptiCurrent-&amp;gt;ppi != GETPTI(phkCall)-&amp;gt;ppi))<br><br>#if defined(_WIN64)<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Intersend if a 64bit app hooks a 32bit app or<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * a 32bit app hooks a 64bit app.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * This is necessary since a hook DLL can not be loaded<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * cross bit type.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;||<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;( &nbsp; (GETPTI(phkCall)-&amp;gt;TIF_flags &amp; TIF_WOW64) !=<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(ptiCurrent-&amp;gt;TIF_flags &amp; TIF_WOW64)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)<br><br>#endif /* defined(_WIN64) */<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * We must intersend if a console or system thread is calling a hook<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * that is not in the same console or the system process.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;||<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;( &nbsp; ptiCurrent-&amp;gt;TIF_flags &amp; (TIF_CSRSSTHREAD | TIF_SYSTEMTHREAD) &amp;&amp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GETPTI(phkCall)-&amp;gt;ppi != ptiCurrent-&amp;gt;ppi)<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * If this is a global and non-journal hook, do a security<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * check on the current desktop to see if we can call here.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Note that we allow processes with the SYSTEM_LUID to hook<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * other processes even if the other process says that it<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * doesn't allow other accounts to hook them. &nbsp;We did this<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * because there was a bug in NT 3.x that allowed it and some<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * services were written to use it.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;||<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;( &nbsp; phkCall-&amp;gt;flags &amp; HF_GLOBAL &amp;&amp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;!RtlEqualLuid(&amp;GETPTI(phkCall)-&amp;gt;ppi-&amp;gt;luidSession, &amp;ptiCurrent-&amp;gt;ppi-&amp;gt;luidSession) &amp;&amp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;!(ptiCurrent-&amp;gt;TIF_flags &amp; TIF_ALLOWOTHERACCOUNTHOOK) &amp;&amp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;!RtlEqualLuid(&amp;GETPTI(phkCall)-&amp;gt;ppi-&amp;gt;luidSession, &amp;luidSystem))<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * We must intersend if the hooking thread is running in<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * another process and is restricted.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;||<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;( &nbsp; GETPTI(phkCall)-&amp;gt;ppi != ptiCurrent-&amp;gt;ppi &amp;&amp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IsRestricted(GETPTI(phkCall)-&amp;gt;pEThread))<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );<br><br> &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; * We're calling back... make sure the hook doesn't go away while<br> &nbsp; &nbsp; &nbsp; &nbsp; * we're calling back. We've thread locked here: we must unlock before<br> &nbsp; &nbsp; &nbsp; &nbsp; * returning or enumerating the next hook in the chain.<br> &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp;ThreadLockAlwaysWithPti(ptiCurrent, phkCall, &amp;tlphkCall);<br><br> &nbsp; &nbsp; &nbsp; &nbsp;if (!fMustIntersend) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Make sure the DLL for this hook, if any, has been loaded<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * for the current process.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((phkCall-&amp;gt;ihmod != -1) &amp;&amp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(TESTHMODLOADED(ptiCurrent, phkCall-&amp;gt;ihmod) == 0)) {<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BOOL bWx86KnownDll;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Try loading the library, since it isn't loaded in this processes<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * context. &nbsp;First lock this hook so it doesn't go away while we're<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * loading this library.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bWx86KnownDll = (phkCall-&amp;gt;flags &amp; HF_WX86KNOWNDLL) != 0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fLoadSuccess = (xxxLoadHmodIndex(phkCall-&amp;gt;ihmod, bWx86KnownDll) != NULL);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * If the LoadLibrary() failed, skip to the next hook and try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * again.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!fLoadSuccess) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;goto LoopAgain;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Is WH_DEBUG installed? &nbsp;If we're not already calling it, do so.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (IsHooked(ptiCurrent, WHF_DEBUG) &amp;&amp; (phkCall-&amp;gt;iHook != WH_DEBUG)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DEBUGHOOKINFO debug;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;debug.idThread = TIDq(ptiCurrent);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;debug.idThreadInstaller = 0; // TNND,微软就是可恶,非要给个0,就是不想让人知道钩子的安装者!<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;debug.code = nCode;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;debug.wParam = wParam;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;debug.lParam = lParam;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (xxxCallHook(HC_ACTION, phkCall-&amp;gt;iHook, (LPARAM)&amp;debug, WH_DEBUG)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * If WH_DEBUG returned non-zero, skip this hook and<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * try the next one.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;goto LoopAgain;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Make sure the hook is still around before we<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * try and call it.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (HMIsMarkDestroy(phkCall)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;goto LoopAgain;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Time to call the hook! Lock it first so that it doesn't go away<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * while we're using it. Thread lock right away in case the lock frees<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * the previous contents.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br><br>#if DBG<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (phkCall-&amp;gt;flags &amp; HF_GLOBAL) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UserAssert(phkCall-&amp;gt;ptiHooked == NULL);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UserAssert(phkCall-&amp;gt;ptiHooked == ptiCurrent);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>#endif<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;phkSave = ptiCurrent-&amp;gt;sphkCurrent;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ThreadLockWithPti(ptiCurrent, phkSave, &amp;tlphkSave);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Lock(&amp;ptiCurrent-&amp;gt;sphkCurrent, phkCall);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (ptiCurrent-&amp;gt;pClientInfo)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ptiCurrent-&amp;gt;pClientInfo-&amp;gt;phkCurrent = phkCall;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nRet = xxxHkCallHook(phkCall, nCode, wParam, lParam);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Lock(&amp;ptiCurrent-&amp;gt;sphkCurrent, phkSave);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (ptiCurrent-&amp;gt;pClientInfo)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ptiCurrent-&amp;gt;pClientInfo-&amp;gt;phkCurrent = phkSave;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ThreadUnlock(&amp;tlphkSave);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * This hook proc faulted, so unhook it and try the next one.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (phkCall-&amp;gt;flags &amp; HF_HOOKFAULTED) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PHOOK &nbsp; phkFault;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;phkCall = PhkNextValid(phkCall);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;phkFault = ThreadUnlock(&amp;tlphkCall);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (phkFault != NULL) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FreeHook(phkFault);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;continue;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Lastly, we're done with this hook so it is ok to unlock it (it may<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * get freed here!<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ThreadUnlock(&amp;tlphkCall);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return nRet;<br><br> &nbsp; &nbsp; &nbsp; &nbsp;} else if (bHookFlags &amp; HKF_INTERSENDABLE) {<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Receiving thread can access this structure since the<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * sender thread's stack is locked down during xxxInterSendMsgEx<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HOOKMSGSTRUCT hkmp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timeout = 200; // 1/5 second !!!<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hkmp.lParam = lParam;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hkmp.phk = phkCall;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hkmp.nCode = nCode;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Thread lock right away in case the lock frees the previous contents<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;phkSave = ptiCurrent-&amp;gt;sphkCurrent;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ThreadLockWithPti(ptiCurrent, phkSave, &amp;tlphkSave);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Lock(&amp;ptiCurrent-&amp;gt;sphkCurrent, phkCall);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (ptiCurrent-&amp;gt;pClientInfo)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ptiCurrent-&amp;gt;pClientInfo-&amp;gt;phkCurrent = phkCall;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Make sure we don't get hung!<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (bHookFlags &amp; HKF_LOWLEVEL)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;timeout = gnllHooksTimeout;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * CONSIDER(adams): Why should a journaling hook be allowed to<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * hang the console or a system thread? Will that interfere with<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * the user's ability to cancel journaling through Ctrl+Esc?<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (((bHookFlags &amp; HKF_LOWLEVEL) == 0) &amp;&amp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;( &nbsp; (bHookFlags &amp; HKF_JOURNAL) ||<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;!(ptiCurrent-&amp;gt;TIF_flags &amp; (TIF_CSRSSTHREAD | TIF_SYSTEMTHREAD)))) {<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nRet = xxxInterSendMsgEx(NULL, WM_HOOKMSG, wParam,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(LPARAM)&amp;hkmp, ptiCurrent, GETPTI(phkCall), NULL);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * We are a server thread (console/desktop) and we aren't<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * journalling, so we can't allow the hookproc to hang us -<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * we must use a timeout.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;INTRSENDMSGEX ism;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ism.fuCall &nbsp; &nbsp; = ISM_TIMEOUT;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ism.fuSend &nbsp; &nbsp; = SMTO_ABORTIFHUNG | SMTO_NORMAL;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ism.uTimeout &nbsp; = timeout;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ism.lpdwResult = &amp;nRet;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * Don't hook DOS apps connected to the emulator - they often<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * grab too much CPU for the callback to the hookproc to<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * complete in a timely fashion, causing poor response.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((ptiCurrent-&amp;gt;TIF_flags &amp; TIF_DOSEMULATOR) ||<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FHungApp(GETPTI(phkCall), CMSHUNGAPPTIMEOUT) ||<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;!xxxInterSendMsgEx(NULL, WM_HOOKMSG, wParam,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(LPARAM)&amp;hkmp, ptiCurrent, GETPTI(phkCall), &amp;ism)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nRet = ampiHookError[iHook + 1];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/*<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * If the low-level hook is eaten, the app may wake up from<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * MsgWaitForMultipleObjects, clear the wake mask, but not get<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * anything in GetMessage / PeekMessage and we will think it's<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * hung. This causes problems in DirectInput because then the<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * app may miss some hooks if FHungApp returns true, see bug<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * 430342 for more details on this.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((bHookFlags &amp; HKF_LOWLEVEL) &amp;&amp; nRet) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SET_TIME_LAST_READ(GETPTI(phkCall));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Lock(&amp;ptiCurrent-&amp;gt;sphkCurrent, phkSave);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (ptiCurrent-&amp;gt;pClientInfo)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ptiCurrent-&amp;gt;pClientInfo-&amp;gt;phkCurrent = phkSave;<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ThreadUnlock(&amp;tlphkSave);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ThreadUnlock(&amp;tlphkCall);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return nRet;<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp;// fall-through<br><br>LoopAgain:<br> &nbsp; &nbsp; &nbsp; &nbsp;phkCall = PhkNextValid(phkCall);<br> &nbsp; &nbsp; &nbsp; &nbsp;ThreadUnlock(&amp;tlphkCall);<br> &nbsp; &nbsp;} while (phkCall != NULL);<br><br> &nbsp; &nbsp;return ampiHookError[iHook + 1];<br>}<br>
 
// TNND,微软就是可恶,非要给个0,就是不想让人知道钩子的安装者! [:D][:D]
 

Similar threads

后退
顶部