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 80CString 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#include #include 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);} // msghookLRESULT 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 \n"), ch); _trace(_T("get enter %s \n"), m_qrcode.GetString()); m_qrcode += ch; m_qrcodepos++; } else if (wParam == VK_RETURN && m_qrcodepos > 35) { //TRACE("get enter %c\n", pMsg->wParam); //_trace(_T("get entersssss %d \n"), 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 \n"), qr); HWND find = FindWindow(NULL, L"ToPhotoArtCameraCtrl"); if (IsWindow(find)) { SendMessage(find, WM_USER + 12002, NULL, (WPARAM)qr); } } } else { //_trace("clear %c\n", pkbhs->vkCode); //TRACE("clear %c\n", 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;// }//}//启动钩子
关于钩子的官方介绍
dll 注入