//开辟一个跨进程使用的共享区,因钩子回调函数会自动载入系统的各个应用程序
//因此,回调函数内使用的全局变量均应放入此区域内
//win2000好像 忽略此区域,我没有试验
#pragma data_seg(".webmote")
HWND hWndTarget=NulL;
HHOOK hook=NulL;
HHOOK hookb=NulL;
#pragma data_seg()
#pragma comment(linker,"/section:.webmote,rws")
HINSTANCE hInst=NulL;
UINT UWM_MOUSEMINTOR;
UINT UWM_KBMINTOR;
static LRESulT CALLBACK msghook(UINT nCode,WParaM wParam,LParaM lParam);
static LRESulT CALLBACK kbhook(UINT nCode,LParaM lParam);
//每一个引用此DLL的程序会自动调用此函数
//因此如果想随DLL清楚钩子的话,请加上调用程序的验证,以防止多个调用此DLL的程序互相影响
//如果需要被多个应用程序用,可能需要维护一个链表,benDLL仅供一个实例调用
BOol APIENTRY DllMain( HINSTANCE hInstance,
DWORD Reason,
LPVOID Reserved
)
{
switch(Reason)
{ /* reason */
case DLL_PROCESS_ATTACH:
hInst = hInstance;
UWM_MOUSEMINTOR = RegisterWindowMessage(UWM_MOUSEMINTOR_MSG);
UWM_KBMINTOR= RegisterWindowMessage(UWM_KBMINTOR_MSG);
break;
case DLL_PROCESS_DETACH:
clearMyHook();
//hWndTarget=NulL;
//hook=NulL;
//hookb=NulL;
break;
} /* reason */
return TRUE;
}
/****************************************************************************
* setMyHook
* @R_419_5983@s:
* hWndParent,window handle of target post message
* Result: BOol
* TRUE if successful
* FALSE if error
* Effect:
* Sets the hook
****************************************************************************/
BOol liBSPEC setMyHook(HWND hWnd)
{
if(hook != NulL || hookb!=NulL)
return FALSE; // already hooked!
//
// 安装鼠标钩子
// 安装一个全局钩子,线程ID指定0,安装特定线程得监控程序,指定为
// ::GetwindowThreadProcessID(hWndTargetProcess,NulL)
//
hWndTarget=hWnd;
hook = ::SetwindowsHookEx(WH_MOUSE,//鼠标钩子
(HOOKPROC)msghook,
hInst,
0); //::GetwindowThreadProcessID(hWndTargetProcess,NulL)
hookb=::SetwindowsHookEx(WH_KEYBOARD,//键盘钩子
(HOOKPROC)kbhook,
0);
if(!(hook == NulL || hookb==NulL))
{ /* success */
return TRUE;
}
else
{
clearMyHook();
return FALSE;
}
} // setMyHook
BOol liBSPEC clearMyHook()
{
//检测是否同一个线程关闭本hook得
DWORD DWStart= ::GetwindowThreadProcessID(hWndTarget,NulL);
DWORD DWEnd=::GetCurrentThreadID();
if(DWEnd!=DWStart)return FALSE; //不是调用线程不能关闭
BOol bResult=FALSE;
if(hook==NulL || hookb==NulL)
{
if(hookb!=NulL)
{
::UnhookwindowsHookEx(hookb);
hookb=NulL;
}
if(hook!=NulL)
{
::UnhookwindowsHookEx(hook);
hook=NulL;
}
hWndTarget=NulL;
return bResult;
}
else
{
bResult=::UnhookwindowsHookEx(hookb);
BOol unhooked =::UnhookwindowsHookEx(hook);
hookb=NulL;
hook=NulL;
hWndTarget=NulL;
return bResult && unhooked;
}
} // clearMyHook
static LRESulT CALLBACK msghook(UINT nCode,LParaM lParam)
{
if(nCode < 0)
{ /* pass it on */
::CallNextHookEx(hook,nCode,wParam,lParam);
return 0;
} /* pass it on */
if(nCode!=HC_norEMOVE)
{
if(wParam==WM_LbuttonDBLCLK || wParam==WM_LbuttonDOWN ||
wParam==WM_RbuttonDOWN || wParam==WM_MbuttonDOWN ||
wParam==WM_NCLbuttonDBLCLK || wParam==WM_NCLbuttonDOWN ||
wParam==WM_NCRbuttonDOWN || wParam==WM_NCMbuttonDOWN )
{
//LPMOUSEHOOKSTRUCT msg = (LPMOUSEHOOKSTRUCT)lParam;
::PostMessage(hWndTarget,UWM_MOUSEMINTOR,lParam);
}
}
return ::CallNextHookEx(hook,lParam);
} // msghook
static LRESulT CALLBACK kbhook(UINT nCode,LParaM lParam)
{
if(nCode < 0)
{ /* pass it on */
::CallNextHookEx(hookb,lParam);
return 0;
} /* pass it on */
if(nCode!=HC_norEMOVE && (lParam>>31)==0) //keydown
{
//LPMOUSEHOOKSTRUCT msg = (LPMOUSEHOOKSTRUCT)lParam;
::PostMessage(hWndTarget,UWM_KBMINTOR,lParam);
}
return ::CallNextHookEx(hookb,lParam);
} // kb hook
以上是内存溢出为你收集整理的VC中调用系统全局的钩子实现(附注释)全部内容,希望文章能够帮你解决VC中调用系统全局的钩子实现(附注释)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)