DLL钩子


// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the HOOK_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// HOOK_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef HOOK_EXPORTS
#define HOOK_API __declspec(dllexport)
#else
#define HOOK_API __declspec(dllimport)
#endif

// This class is exported from the hook.dll
class HOOK_API CHook {
public:
    CHook(void);
    // TODO: add your methods here.
};

extern HOOK_API int nHook;

HOOK_API int fnHook(void);



// hook.cpp : Defines the entry point for the DLL application.

//

#include "stdafx.h"
#include "hook.h"
#include "windows.h"

#define WM_MOUSEHOOK WM_USER+6
#pragma   data_seg("Shared")
HHOOK glhHook = NULL;//安装的鼠标钩子句柄
HINSTANCE glhInstance = NULL;//DLL实例句柄
HWND glhDisplayWnd = NULL;
HWND glhPrevTarWnd = NULL;
#pragma   data_seg()
#pragma   comment(linker,"/SECTION:Shared,RWS")  

BOOL APIENTRY DllMain(HANDLE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
    )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}


// This is an example of an exported variable
HOOK_API int nHook = 0;

// This is an example of an exported function.
HOOK_API int fnHook(void)
{
    return 42;
}

// This is the constructor of a class that has been exported.
// see hook.h for the class definition
CHook::CHook()
{
    return;
}

LRESULT CALLBACK WINAPI MouseProc(int nCode, WPARAM wparam, LPARAM lparam)

{


    if (WM_LBUTTONDOWN != wparam) return 0;
    LPMOUSEHOOKSTRUCT ps = (LPMOUSEHOOKSTRUCT)lparam;
    //HWND hWnd = WindowFromPoint(ps->pt);
    HWND hWnd = ps->hwnd;//取目标窗口句柄
    PostMessage(glhDisplayWnd, WM_MOUSEHOOK, (WPARAM)hWnd, 0);
    //SendMessage(hWnd, WM_LBUTTONDBLCLK, (WPARAM)hWnd, 0);
    //if (nCode < 0)    
    return CallNextHookEx(glhHook, nCode, wparam, lparam);
    //MessageBox(NULL, TEXT("鼠标钩子"), TEXT("错误"), 0);
    //return 0;

}
//启动钩子
_declspec(dllexport) void StartHook(HWND hWnd)
{
    glhInstance = GetModuleHandle("hook");

    glhHook = SetWindowsHookEx(WH_MOUSE, MouseProc, glhInstance, 0); //全局钩子最后一个函数要为零

    glhDisplayWnd = hWnd;
}

//卸载钩子
_declspec(dllexport) void StopHook()
{
    //MessageBox(NULL,TEXT("卸载钩子"),TEXT("错误"),0);
    BOOL bResult;
    bResult = UnhookWindowsHookEx(glhHook);
    if (bResult)
    {
        glhDisplayWnd = NULL;
        glhHook = NULL;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DLL注入是指在目标进程中加载一个外部的DLL文件,并且在目标进程的地址空间中执行该DLL中的函数。在进行DLL注入时,我们可以利用钩子函数技术来实现一些特殊的功能。 下面是一个简单的DLL注入钩子函数的实现过程: 1. 定义一个全局的钩子函数: ```c++ LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode < 0) { return CallNextHookEx(NULL, nCode, wParam, lParam); } // 进行一些特殊的处理 return CallNextHookEx(NULL, nCode, wParam, lParam); } ``` 2. 在DLL的入口函数中,使用SetWindowsHookEx函数安装钩子函数: ```c++ // 安装钩子函数 HHOOK hHook = SetWindowsHookEx(WH_CALLWNDPROC, HookProc, hDll, 0); if (hHook == NULL) { // 处理错误信息 } // 进行消息循环 MSG msg; while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } // 卸载钩子函数 UnhookWindowsHookEx(hHook); ``` 3. 在目标进程中,调用LoadLibrary函数加载DLL文件: ```c++ // 加载DLL文件 HMODULE hDll = LoadLibrary(L"path/to/DLL"); if (hDll == NULL) { // 处理错误信息 } ``` 通过以上步骤,我们可以在目标进程中成功注入一个DLL,并且在其中安装一个钩子函数。在钩子函数中,我们可以进行一些特殊的处理,例如记录键盘输入、拦截鼠标消息等等。需要注意的是,钩子函数的处理需要尽可能地简洁和高效,以避免对目标进程造成不必要的影响。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值