首页 > 其他 > 详细

HOOK - 消息钩子

时间:2016-10-21 15:37:08      阅读:310      评论:0      收藏:0      [点我收藏+]
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    全局钩子: 监控程序的最大化.最小化
//
#ifndef        __HOOK_WINDOW_H_2016_10_12__
#define        __HOOK_WINDOW_H_2016_10_12__


#ifdef        __cplusplus
#define        EXPORT extern "C" __declspec(dllexport)
#else
#define        EXPORT __declspec(dllexport)
#endif    // __cplusplus


// 安装钩子
EXPORT void SetHook(HWND hGameWnd);
// 卸载钩子
EXPORT void UnSetHook(void);

#endif

//========================================
#include <atlstr.h>
#include <Windows.h>
#include <tchar.h>
#include "fkDLL.h"


// 共享数据
#pragma data_seg("ShardData")
HWND g_hGameWnd = NULL;
#pragma data_seg()
#pragma comment(linker,"/SECTION:ShardData,RWS")

static HINSTANCE g_hInstance = NULL;
static HHOOK g_hHook = NULL;
static HHOOK g_hMsg = NULL;

// WH_GETMESSAGE
LRESULT CALLBACK GetMsgProc(int iCode, WPARAM wParam, LPARAM lParam)
{
    if (iCode < 0 || iCode == HC_NOREMOVE)
    {
        return CallNextHookEx(g_hMsg, iCode, wParam, lParam);
    }

    PMSG pMsg = (PMSG)lParam;
    if (g_hGameWnd == pMsg->hwnd)
    {
        switch (pMsg->message)
        {
        case WM_SYSCOMMAND:
            if (SC_MINIMIZE == pMsg->wParam) //截获最小化
            {
                OutputDebugString(_T("GameTips:    -    截获了最小化消息"));
                pMsg->message = WM_NULL;    //将消息重置为WM_NULL,就达到截获的目的.
            }
            else if (SC_CLOSE == pMsg->wParam)
            {
                OutputDebugString(_T("GameTips:    -    截获了关闭消息"));
                pMsg->message = WM_NULL;    //将消息重置为WM_NULL,就达到截获的目的.
            }
            break;
            
        case WM_NCLBUTTONDOWN:
            if (HTMINBUTTON == pMsg->wParam)
            {
                OutputDebugString(_T("GameTips:    -    截获了最小化按钮消息"));
                pMsg->message = WM_NULL;    //将消息重置为WM_NULL,就达到截获的目的.
            }
            else if (HTCLOSE == pMsg->wParam)
            {
                OutputDebugString(_T("GameTips:    -    截获了关闭按钮消息"));
                pMsg->message = WM_NULL;    //将消息重置为WM_NULL,就达到截获的目的.
            }
            break;

        case WM_KEYDOWN:
            if (VK_F11 == pMsg->wParam)
            {
                OutputDebugString(_T("GameTips:    -    截获了F11"));
                pMsg->message = WM_NULL;    //将消息重置为WM_NULL,就达到截获的目的.
            }
        }
    }
    return CallNextHookEx(g_hMsg, iCode, wParam, lParam);
}

// 安装钩子
EXPORT void SetHook(HWND hGameWnd)
{
    if (g_hMsg == NULL)
    {
        g_hGameWnd = hGameWnd;
        g_hMsg = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)GetMsgProc, g_hInstance, 0);
    }
}

// 卸载钩子
EXPORT void UnSetHook(void)
{
    if (g_hMsg)
    {
        UnhookWindowsHookEx(g_hMsg);
        g_hMsg = NULL;
    }
}


// 钩子主函数
int WINAPI DllMain(HINSTANCE hDllInstance, DWORD dwReason, LPVOID lpreserved)
{

    switch (dwReason)
    {
    case DLL_PROCESS_ATTACH:
        //保存实例句柄
        g_hInstance = hDllInstance;
        break;

    case DLL_THREAD_ATTACH:

        break;

    case DLL_THREAD_DETACH:
        break;

    case DLL_PROCESS_DETACH:
        break;
    }

    return TRUE;
}

=========================================
XXDLG调用:
//安装钩子
void XXDLG::InstallHook(HWND hWnd)
{
    // 安装CBT钩子
    if (m_hDLL == NULL)
    {
        m_hDLL = LoadLibrary(_T("fuckDLL.dll"));
        ASSERT(m_hDLL);
        typedef void(*SET_HOOK)(HWND);
        SET_HOOK pSetHook = (SET_HOOK)GetProcAddress(m_hDLL, "SetHook");
        ASSERT(pSetHook);
        pSetHook(hWnd);
    }
}

//卸载钩子
void XXDLG::UnInstallHook()
{
    if (m_hDLL)
    {
        typedef void(*UN_SET_HOOK)(void);
        UN_SET_HOOK pUnSetHook = (UN_SET_HOOK)GetProcAddress(m_hDLL, "UnSetHook");
        ASSERT(pUnSetHook);
        pUnSetHook();
        FreeLibrary(m_hDLL);
        m_hDLL = NULL;
    }
}

 

HOOK - 消息钩子

原文:http://www.cnblogs.com/DuanLaoYe/p/5984658.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!