windows hook(钩子)--进程钩子

先了解windows消息机制原理:

地址:

windows消息机制了解和代码实战_qq_1410888563的博客-CSDN博客

1.hook处理windows消息机制原理

windows hook(钩子)--进程钩子_第1张图片

 2.钩子分为两种

1.进程内钩子:只对当前进程起作用

2.全局钩子:对系统中所有的进程起作用

1.进程钩子代码

#include 


//回调函数
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMSg, WPARAM wParam, LPARAM lParam);

//钩子处理函数
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam);
//钩子处理函数
LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam);
//winmain:相当于main函数,入口函数,窗口运用
// HINSTANCE:运用程序实例句柄
//LPSTR:char*

HHOOK g_mouseHook;
HHOOK g_keyboardHook;
HWND g_hwnd;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow)
{
    //做一个窗口,工具:spy++
    //1.设计窗口类
    TCHAR name[] = TEXT("AppTest");

    WNDCLASS winclass{0};
    winclass.cbClsExtra = 0;//窗口类的额外空间大小
    winclass.cbWndExtra = 0;//窗口的额外空间大小
    winclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //白色画刷
    winclass.hCursor = LoadCursor(NULL, IDC_ARROW);//光标句柄
    winclass.hIcon = NULL;         //图标句柄
    winclass.hInstance = hInstance; //当前运用程序句柄
    winclass.lpfnWndProc = WindowProc;//窗口处理函数
    winclass.lpszClassName = name;//窗口类类型名
    winclass.lpszMenuName = NULL;
    winclass.style = CS_HREDRAW | CS_VREDRAW;//窗口类的风格

    //2.注册窗口类
    RegisterClass(&winclass);

    //3.创建窗口
    g_hwnd = CreateWindow(name, TEXT("进大厂"), WS_OVERLAPPEDWINDOW, 400, 100, 800, 600, NULL, NULL, hInstance, NULL);

    //4.显示窗口
    ShowWindow(g_hwnd,SW_SHOW);

    //5.更新窗口
    UpdateWindow(g_hwnd);

    //6.消息循环
    //windows运用程序是通过消息机制驱动运行
    MSG msg;
    while (GetMessage(&msg,NULL,0,0))
    {
        //将虚拟键消息转换成字符消息
        TranslateMessage(&msg);
        //将消息分发给窗口处理函数
        DispatchMessage(&msg);
    }
    return 0;
}


//1.进程内钩子:只对当前进程起作用
//监控鼠标:安装鼠标钩子
// 
//回调函数
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMSg, WPARAM wParam, LPARAM lParam)
{
    switch (uMSg)
    {
    case WM_CREATE://窗口创建消息
        g_mouseHook =SetWindowsHookEx(WH_MOUSE, MouseProc,NULL,GetCurrentThreadId());//鼠标钩子
        g_keyboardHook = SetWindowsHookEx(WH_KEYBOARD, KeyBoardProc, NULL, GetCurrentThreadId());//键盘钩子
        break;
    case WM_KEYDOWN:
        MessageBox(hWnd, TEXT("键盘按下"), TEXT("提示"), MB_OK);
        break;
    case WM_LBUTTONDOWN://鼠标按下消息
        MessageBox(hWnd, TEXT("鼠标按下"), TEXT("提示"), MB_OK);
        break;
    case WM_CLOSE://窗口关闭消息
        DestroyWindow(hWnd);
        break;
    case WM_DESTROY: //消息销毁消息
        PostQuitMessage(0);
        break;
    default:
        break;
    }
    return DefWindowProc(hWnd, uMSg, wParam, lParam);
}

//钩子处理函数
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{

    //return 1;//表示对当前消息已经处理了,并且屏蔽了,消息就不会传递了
    return CallNextHookEx(g_mouseHook, nCode, wParam, lParam);//让hook钩子传递下去
}

//钩子处理函数
LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    //只屏蔽空格键
    if (wParam == VK_SPACE)
        return 1;
    else
        return CallNextHookEx(g_keyboardHook, nCode, wParam, lParam);//让hook钩子传递下去
    return 1;//表示对当前消息已经处理了,并且屏蔽了,消息就不会传递了
    //return CallNextHookEx(g_keyboardHook, nCode, wParam, lParam);//让hook钩子传递下去
}

你可能感兴趣的:(C++经验,c++)