要截获系统中所有应用程序的消息,必须将钩子函数写在动态连接库中,否则钩子只能截获该应用程序的消息。其实,这个道理很简单,我们看下面这个安装钩子的API:
HHOOK SetWindowsHookEx(
int idHook, // type of hook to install
HOOKPROC lpfn, // address of hook procedure
HINSTANCE hMod, // handle to application instance
DWORD dwThreadId // identity of thread to install hook for
);
注意第三个参数(HINSTANCE hMod)和第四个参数(DWORD dwThreadId),这两个参数指明了一个dll实例句柄和需要截获消息的线程ID。如果hMod为NULL,则必须指定dwThreadId;如果dwThreadId为0则说明钩子对所有线程有效,但此时你必须指定hMod,这是因为一个dll就是一个独立的线程,它独立于其他应用程序,因此可以截获其他线程的消息。
下面这个函数实现了钩子安装:
/////////////////////////////////////////////////////////////////////////////
// The KeyBoard Hook Install Function Implement
DllExport void WINAPI InstallHook()
{
g_KeyBoardHook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardHookProc,
theApp.m_hInstance,0);
g_MouseHook = SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseHookProc,
theApp.m_hInstance,0);
}
其中,theApp是dll类的全局对象。
下面这个函数实现了钩子的卸载:
/////////////////////////////////////////////////////////////////////////////
// The KeyBoard Hook UnInstall Function Implement
DllExport void WINAPI UnInstallHook()
{
UnhookWindowsHookEx(g_KeyBoardHook);
UnhookWindowsHookEx(g_MouseHook);
}
下面两个函数分别是键盘和鼠标的钩子函数:
/////////////////////////////////////////////////////////////////////////////
// The KeyBoard & Mouse Hook Function Implement
DllExport LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode == HC_ACTION)
{
switch(wParam)
{
case VK_SPACE:
if(((GetKeyState(VK_LCONTROL) & 0x8000) != 0) && ((GetKeyState(VK_RSHIFT) & 0x8000) != 0))
{
bEatKeystroke = FALSE;
bEatMousestroke = FALSE;
}
break;
}
}
return(bEatKeystroke ? 1 : CallNextHookEx(g_KeyBoardHook,nCode,wParam,lParam));
}
DllExport LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
return(bEatMousestroke ? 1 : CallNextHookEx(g_MouseHook,nCode,wParam,lParam));
}
很简单,这里只是判断是否按下了左Ctrl+右Shift+空格,如果是就解锁。
最后,在应用程序中,引入该dll的lib,然后在应用程序的恰当位置调用上面函数安装hook,在适当的位置Uninstall即可。
下面是一个能够在一个时间段锁住键盘和鼠标完整的应用程序:
//TimerLock.h
// head file
// declare application class and window class
// LionMountainMan
// 2005-8-25
class CMyApp : public CWinApp
{
protected:
CTime m_time;
public:
virtual BOOL InitInstance();
virtual int Run();
};
/////////////////////////////////////////////////////////////////////////////////////
// TimerLock.cpp
// implement file
// implement the timer lock keyboard and mouse function.
// LionMountainMan
// 2005-8-25
#include <afxwin.h>
#include "TimerLock.h"
#include "HookDll.h"
CMyApp myApp;
/////////////////////////////////////////////////////////////////////////////////////
//CMyApp member functions
BOOL CMyApp::InitInstance()
{
m_time = CTime::GetCurrentTime();
return TRUE;
}
int CMyApp::Run()
{
BOOL bLock = FALSE;
CTime tmAlarm1(m_time.GetYear(),m_time.GetMonth(), m_time.GetDay() ,10,30,0);
CTime tmCancelAlarm1(m_time.GetYear(),m_time.GetMonth(), m_time.GetDay() ,10,40,0);
CTime tmAlarm2(m_time.GetYear(),m_time.GetMonth(), m_time.GetDay() ,15,30,0);
CTime tmCancelAlarm2(m_time.GetYear(),m_time.GetMonth(), m_time.GetDay() ,15,40,0);
CTime tmAlarm3(m_time.GetYear(),m_time.GetMonth(), m_time.GetDay(),12,30,0);
CTime tmCancelAlarm3(m_time.GetYear(),m_time.GetMonth(),
m_time.GetDay(),13,30,0);
CTime tmAlarm4(m_time.GetYear(),m_time.GetMonth(), m_time.GetDay(),17,30,0);
CTime tmCancelAlarm4(m_time.GetYear(),m_time.GetMonth(), m_time.GetDay(),18,0,0);
while(1)
{
m_time = CTime::GetCurrentTime();
if((tmAlarm1 <= m_time && tmCancelAlarm1 >= m_time) || (tmAlarm2 <= m_time && tmCancelAlarm2 >= m_time) || (tmAlarm3 <= m_time && tmCancelAlarm3 >= m_time) || (tmAlarm4 <= m_time && tmCancelAlarm4 >= m_time))
{
if(!bLock)
{
InstallHook();
MessageBox(NULL,"时间到,停止一切操作!","温馨提示",MB_ICONSTOP|MB_TOPMOST);
bLock = TRUE;
}
}
else if(bLock)
{
UnInstallHook();
bLock = FALSE;
}
Sleep(1000);
}
return 0;
}
LionMountainMan
September 1, 2005 .CD