HOOK - 消息钩子

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    全局钩子: 监控程序的最大化.最小化
//
#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;
    }
}

 

posted @ 2016-10-21 14:59  C/C++/Python/Java  阅读(2735)  评论(0编辑  收藏  举报