全局HOOK要做成DLL模块,被系统调用,从而实现全局监控。在DLL中要实现HOOK处理函数、 SetWindowsHookEx和UnhookWindowsHookEx函数。
一、VC建立DL:L工程。
二、添加自己的函数(监控某窗口的键盘消息,实现屏蔽系统键)
添加的全局键盘钩子代码:
//
HWND m_hLWnd=NULL; //监视的窗口句柄
HWND m_hPWnd=NULL; //处理该消息的窗口句柄
HHOOK g_hHookKbdLL; //HOOK句柄
extern "C" _declspec(dllexport) LRESULT CALLBACK GlobalKeyHOOK(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // mouse coordinates
)
{
HWND hwnd=GetActiveWindow();
if(hwnd==m_hLWnd)
{
KBDLLHOOKSTRUCT* pKBDLLHOOK=(KBDLLHOOKSTRUCT*)lParam;
if(pKBDLLHOOK->flags&0x80)//按下
{
PostMessage (m_hPWnd, WM_KEYUP,pKBDLLHOOK->vkCode,0);
}
else//释放
{
PostMessage (m_hPWnd, WM_KEYDOWN,pKBDLLHOOK->vkCode,0);
}
return 1;
}
return 0;
}
extern "C" _declspec(dllexport) void SetHOOKWindow(HWND ListenHwnd,HWND ProHwnd)
{
m_hLWnd=ListenHwnd;
m_hPWnd=ProHwnd;
}
//
extern "C" _declspec(dllexport) void SetHOOK()
{
g_hHookKbdLL = SetWindowsHookEx(WH_KEYBOARD_LL,GlobalKeyHOOK,
(HINSTANCE__ *)m_hModule, 0); //m_hModule是从模块入口函数DllMain()的模块句柄参数赋值而得
}
extern "C" _declspec(dllexport) void UnSetHOOK()
{
if(g_hHookKbdLL)
{
UnhookWindowsHookEx(g_hHookKbdLL);
}
}
三、调用
1、建立调用工程。
2、调用代码
//声明DLL中将要用到的函数的指针
void (*SetHook)(void);
void (*SetHOOKWindow)(HWND ListenHwnd,HWND ProHwnd);
//具体调用代码
HOOKPROC GlobalMouseProc;
HINSTANCE hinstDLL;
HHOOK MK_HOOK;
hinstDLL = LoadLibrary((LPCTSTR) "G://MyProgram//MKHooks//Debug//MKHooks.dll");
if(NULL==hinstDLL)AfxMessageBox("Load DLL ERROR");
SetHook=(void (__cdecl *)(void))GetProcAddress(hinstDLL, "SetHOOK");
SetHOOKWindow=(void (__cdecl *)(struct HWND__ *,struct HWND__ *))GetProcAddress(hinstDLL, "SetHOOKWindow");
SetHOOKWindow(this->m_hWnd,m_hProWnd);
SetHook();
(Windows HOOK 函数查看MSDN)
一、VC建立DL:L工程。
二、添加自己的函数(监控某窗口的键盘消息,实现屏蔽系统键)
添加的全局键盘钩子代码:
//
HWND m_hLWnd=NULL; //监视的窗口句柄
HWND m_hPWnd=NULL; //处理该消息的窗口句柄
HHOOK g_hHookKbdLL; //HOOK句柄
extern "C" _declspec(dllexport) LRESULT CALLBACK GlobalKeyHOOK(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // mouse coordinates
)
{
HWND hwnd=GetActiveWindow();
if(hwnd==m_hLWnd)
{
KBDLLHOOKSTRUCT* pKBDLLHOOK=(KBDLLHOOKSTRUCT*)lParam;
if(pKBDLLHOOK->flags&0x80)//按下
{
PostMessage (m_hPWnd, WM_KEYUP,pKBDLLHOOK->vkCode,0);
}
else//释放
{
PostMessage (m_hPWnd, WM_KEYDOWN,pKBDLLHOOK->vkCode,0);
}
return 1;
}
return 0;
}
extern "C" _declspec(dllexport) void SetHOOKWindow(HWND ListenHwnd,HWND ProHwnd)
{
m_hLWnd=ListenHwnd;
m_hPWnd=ProHwnd;
}
//
extern "C" _declspec(dllexport) void SetHOOK()
{
g_hHookKbdLL = SetWindowsHookEx(WH_KEYBOARD_LL,GlobalKeyHOOK,
(HINSTANCE__ *)m_hModule, 0); //m_hModule是从模块入口函数DllMain()的模块句柄参数赋值而得
}
extern "C" _declspec(dllexport) void UnSetHOOK()
{
if(g_hHookKbdLL)
{
UnhookWindowsHookEx(g_hHookKbdLL);
}
}
三、调用
1、建立调用工程。
2、调用代码
//声明DLL中将要用到的函数的指针
void (*SetHook)(void);
void (*SetHOOKWindow)(HWND ListenHwnd,HWND ProHwnd);
//具体调用代码
HOOKPROC GlobalMouseProc;
HINSTANCE hinstDLL;
HHOOK MK_HOOK;
hinstDLL = LoadLibrary((LPCTSTR) "G://MyProgram//MKHooks//Debug//MKHooks.dll");
if(NULL==hinstDLL)AfxMessageBox("Load DLL ERROR");
SetHook=(void (__cdecl *)(void))GetProcAddress(hinstDLL, "SetHOOK");
SetHOOKWindow=(void (__cdecl *)(struct HWND__ *,struct HWND__ *))GetProcAddress(hinstDLL, "SetHOOKWindow");
SetHOOKWindow(this->m_hWnd,m_hProWnd);
SetHook();
(Windows HOOK 函数查看MSDN)