关闭

DLL钩子

标签: HOOKDLL
147人阅读 评论(0) 收藏 举报
分类:


// 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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1153次
    • 积分:33
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档