求C++写的用钩子禁用鼠标和键盘的代码
展开全部
#include <windows.h>
#pragma data_seg("MySec")
HWND g_hnd=NULL;
#pragma data_seg()
#pragma comment(linker,"/section:MySec,RWS")
HHOOK g_hmouse = NULL;
HHOOK g_hkeyboard=NULL;
//鼠标钩子过程函数
LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam)
{
return 1;
}
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
if (0x41==wParam && (1==(lParam >> 29 &1)))
{
SendMessage(g_hnd, WM_CLOSE,0,0);
UnhookWindowsHookEx(g_hmouse);
UnhookWindowsHookEx(g_hkeyboard);
}
return 1;
}
//安装钩子函数
void SetHook(HWND hnd)
{
g_hnd=hnd;
g_hmouse = SetWindowsHookEx(WH_MOUSE, MouseProc, GetModuleHandle("HOOk"),0);
g_hkeyboard = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("HOOk"),0);
}
#pragma data_seg("MySec")
HWND g_hnd=NULL;
#pragma data_seg()
#pragma comment(linker,"/section:MySec,RWS")
HHOOK g_hmouse = NULL;
HHOOK g_hkeyboard=NULL;
//鼠标钩子过程函数
LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam)
{
return 1;
}
LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
if (0x41==wParam && (1==(lParam >> 29 &1)))
{
SendMessage(g_hnd, WM_CLOSE,0,0);
UnhookWindowsHookEx(g_hmouse);
UnhookWindowsHookEx(g_hkeyboard);
}
return 1;
}
//安装钩子函数
void SetHook(HWND hnd)
{
g_hnd=hnd;
g_hmouse = SetWindowsHookEx(WH_MOUSE, MouseProc, GetModuleHandle("HOOk"),0);
g_hkeyboard = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle("HOOk"),0);
}
参考资料: 孙鑫深入详解VC++ 第19章
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询