mfc钩子

Posted baldermurphy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mfc钩子相关的知识,希望对你有一定的参考价值。

dll钩子检测二维码扫描器和右击事件

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "MouseHook.h"

#pragma data_seg(".JOE")
HWND hWndServer = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:.JOE,rws")

HINSTANCE hInst;
UINT UWM_MOUSEMOVE;
HHOOK hook;
HHOOK keybroadhook;

#define MAX_QRCODE_LENGTH 80
CString m_qrcode;
//char m_qrcode[MAX_QRCODE_LENGTH];
int m_qrcodepos = 0;
DWORD    m_starttime = 0;

static LRESULT CALLBACK msghook(UINT nCode, WPARAM wParam, LPARAM lParam);
static LRESULT CALLBACK keyhook(UINT nCode, WPARAM wParam, LPARAM lParam);

HANDLE defaultMouse = NULL;
char ch;




#include <stdio.h>
#include <wTypes.h>
#include <tchar.h> 
bool _trace(TCHAR *format, ...)

{

    TCHAR buf[32 * 1024] = { 0 };
    va_list args;
    va_start(args, format);
    wvsprintf(buf, format, args);
    va_end(args);

    OutputDebugString(buf);


    return true;

}



BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    { /* reason */
    case DLL_PROCESS_ATTACH:
        hInst = hModule;
        UWM_MOUSEMOVE = RegisterWindowMessage(UWM_MOUSEMOVE_MSG);
        return TRUE;
    case DLL_PROCESS_DETACH:
        if (hWndServer != NULL)
            clearMyHook(hWndServer);
        return TRUE;
    } /* reason */
    return TRUE;
}


/****************************************************************************
*                                 setMyHook
* Inputs:
*       HWND hWnd: Window to notify
* Result: BOOL
*       TRUE if successful
*    FALSE if error
* Effect:
*       Sets the hook
****************************************************************************/

__declspec(dllexport) BOOL setMyHook(HWND hWnd)
{
    if (hWndServer != NULL)
        return FALSE; // already hooked!
    hook = SetWindowsHookEx(
        WH_GETMESSAGE,
        (HOOKPROC)msghook,
        hInst,
        0);
    keybroadhook= SetWindowsHookEx(
        WH_KEYBOARD,
        (HOOKPROC)keyhook,
        hInst,
        0);
    if (hook != NULL&& keybroadhook!=NULL)
    { /* success */
        hWndServer = hWnd;
        return TRUE;
    } /* success */


    return FALSE; // failed to set hook
} // setMyHook

  /****************************************************************************
  *                                 clearMyHook
  * Inputs:
  *       HWND hWnd: Window hook
  * Result: BOOL
  *       TRUE if successful
  *    FALSE if error
  * Effect:
  *       Removes the hook that has been set
  ****************************************************************************/

__declspec(dllexport) BOOL clearMyHook(HWND hWnd)
{
    if (hWnd != hWndServer || hWnd == NULL)
        return FALSE;
    BOOL unhooked = UnhookWindowsHookEx(hook);
    if (unhooked) {
        hWndServer = NULL;
    }
    bool unhookkeyed = UnhookWindowsHookEx(keybroadhook);
    if (unhookkeyed) {
        keybroadhook = NULL;
    }

    return unhooked;
} // clearMyHook

  /****************************************************************************
  *                                   msghook
  * Inputs:
  *       int nCode: Code value
  *    WPARAM wParam:
  *    LPARAM lParam:
  * Result: LRESULT
  *       Either 0 or the result of CallNextHookEx
  * Effect:
  *       Hook processing function. If the message is a mouse-move message,
  *    posts the coordinates to the parent window
  ****************************************************************************/

static LRESULT CALLBACK msghook(UINT nCode, WPARAM wParam, LPARAM lParam)
{

    if (nCode < 0)
    { /* pass it on */
        CallNextHookEx(hook, nCode, wParam, lParam);
        return 0;
    } /* pass it on */

        LPMSG msg = (LPMSG)lParam;

    if (msg->message == WM_KEYDOWN)
    {
        switch (msg->wParam)
        {
        case VK_UP:
        case VK_DOWN:
        case VK_LEFT:
        case VK_RIGHT:
        case VK_SPACE:
        case q:
        case Q:
        case e:
        case E:
        case a:
        case A:
        case s:
        case S:
        case d:
        case D:
        case W:
        case w:
        {
            HWND wnd = ::FindWindow(NULL, L"TOPHOTOART_SCENE");
            if (IsWindow(wnd))
            {
                PostMessage(wnd, WM_USER + 10005, msg->wParam, msg->lParam);
            }
        }

            break;
        }
        
        //MessageBox(NULL     , L"key down", L"", MB_OK); 
    }
    if (msg->message == WM_RBUTTONDOWN)
    {
        HWND find = FindWindow(NULL, L"ToPhotoArtCameraCtrl");
        if (IsWindow(find))
        {
            SendMessage(find, WM_USER + 10001, 0, 0);
        }
    }


    
    return CallNextHookEx(hook, nCode, wParam, lParam);
} // msghook

LRESULT keyhook(UINT nCode, WPARAM wParam, LPARAM lParam){
    if (nCode < 0){
        CallNextHookEx(hook, nCode, wParam, lParam);
        return 0;
    }
    if (!((DWORD)lParam & 0x40000000) &&
        (HC_ACTION == nCode))
    {
        if ((wParam == VK_RETURN) || (wParam >= 0x2f) && (wParam <= 0x100))
        {
            DWORD tmp = GetTickCount() - m_starttime;
            BYTE ks[256];
            GetKeyboardState(ks);

            WORD w;
            UINT scan = 0;
            ToAscii(wParam, scan, ks, &w, 0);
            ch = char(w);
            if (tmp < 50) {
                if (wParam != VK_RETURN)
                {
                    //_trace(_T("get enter %c 
"), ch);
                    _trace(_T("get enter %s 
"), m_qrcode.GetString());
                    m_qrcode += ch;
                    m_qrcodepos++;
                }
                else if (wParam == VK_RETURN && m_qrcodepos > 35) {
                    //TRACE("get enter %c
", pMsg->wParam);
                    //_trace(_T("get entersssss %d 
"), m_qrcode.GetLength());
                    m_qrcodepos = 0;

                    char qr[MAX_QRCODE_LENGTH];
                    memset(qr, 0, MAX_QRCODE_LENGTH);
                    memcpy(qr, m_qrcode.GetString(), MAX_QRCODE_LENGTH);
                    //_trace(_T("get enter %s 
"), qr);
                    HWND find = FindWindow(NULL, L"ToPhotoArtCameraCtrl");
                    if (IsWindow(find))
                    {
                        SendMessage(find, WM_USER + 12002, NULL, (WPARAM)qr);
                    }
                }
            }
            else {
                //_trace("clear %c
", pkbhs->vkCode);
                //TRACE("clear %c
", pMsg->wParam);
                m_qrcodepos = 0;
                m_qrcode = L"";
                m_qrcode += ch;
                m_qrcodepos++;
            }
            m_starttime = GetTickCount();
        }
    }

    return CallNextHookEx(hook, nCode, wParam, lParam);
}



//LRESULT CALLBACK MouseProc(int nCode, WPARAM wparam, LPARAM lparam)
//{
//    LPMOUSEHOOKSTRUCT pMouseHook =
//        (MOUSEHOOKSTRUCT FAR *)lparam;
//    if (nCode >= 0)
//    {
//        HWND glhTargetWnd =
//            ::WindowFromPoint(pMouseHook->pt);
//        if ((glhTargetWnd != g_hPrevTarWnd) &&
//            (NULL != glhTargetWnd))
//        {
//            char szCaption[256];
//            GetWindowText(glhTargetWnd, szCaption, 100);
//            //取目标窗口标题
//            if (IsWindow(g_hDisplayWnd))
//            {
//                SendMessage(g_hDisplayWnd, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)szCaption);
//            }
//            g_hPrevTarWnd = glhTargetWnd;//保存目标窗口
//        }
//    }
//    return CallNextHookEx(g_hHook, nCode, wparam, lparam);
//    //继续传递消息
//}
//
//_declspec(dllexport) void StartHook(HWND hWnd)
//{
//    g_hHook = SetWindowsHookEx(WH_MOUSE, MouseProc, GetModuleHandle("Hook"), 0);
//    //全局钩子最后一个函数要为零
//    g_hDisplayWnd = hWnd;
//}
////卸载钩子
//_declspec(dllexport) void StopHook()
//{
//    BOOL bResult;
//    bResult = UnhookWindowsHookEx(g_hHook);
//    if (bResult)
//    {
//        g_hPrevTarWnd = NULL;
//        g_hDisplayWnd = NULL;
//        g_hHook = NULL;
//    }
//}
//启动钩子

 关于钩子的官方介绍

https://technet.microsoft.com/zh-cn/library/ms644990(d=printer).aspx

https://docs.microsoft.com/zh-cn/windows/desktop/winmsg/about-hooks#wh_journalrecordhook

虚拟按键 对照表 https://docs.microsoft.com/zh-cn/windows/desktop/inputdev/virtual-key-codes

 

以上是关于mfc钩子的主要内容,如果未能解决你的问题,请参考以下文章

MFC钩子问题 我想点击一个按钮 弹出一个对话框 然后用钩子函数修改消息,点击之后弹出另一个dui对话框

MFC如何改变消息框的三个按钮的文本值

使用 Java 代码操作 MFC 对话框可执行文件

C++ 代码片段(积累)

MFC 如何使窗口 置顶

钩子编程(HOOK) 安装进程内键盘钩子