注入DLL注册热键方案之一:替换窗口过程
1 #include "stdafx.h" 2 #include "HotKey.h" 3 using namespace std; 4 5 HWND CHotKey::m_hMainWnd = NULL; 6 HHOOK CHotKey::m_hMsgHook= NULL; 7 WNDPROC CHotKey::m_OldWndProc = NULL; 8 9 std::vector<HOT_KEY_INFO> CHotKey::m_vecHotKey; 10 11 BOOL CHotKey::InitHotKey() 12 { 13 m_vecHotKey.clear(); 14 15 GetMainWindowHandle(); 16 if (m_hMainWnd == NULL) 17 { 18 return FALSE; 19 } 20 21 m_OldWndProc = (WNDPROC)SetWindowLong(m_hMainWnd, GWL_WNDPROC, (LONG)HookWndProc); 22 23 DWORD threadId = GetWindowThreadProcessId(m_hMainWnd, NULL); 24 25 m_hMsgHook = SetWindowsHookEx(WH_GETMESSAGE, HookProc, GetModuleHandle(NULL), threadId); 26 27 return TRUE; 28 } 29 30 BOOL CHotKey::SetHotKey(VOIDFUNC pfnAddr, DWORD dwVK) 31 { 32 static BOOL bInit = FALSE; 33 if (!bInit) 34 { 35 InitHotKey(); 36 bInit = TRUE; 37 } 38 HOT_KEY_INFO stHotKey = { 0 }; 39 stHotKey.atHotKey = 0; 40 stHotKey.bRegistered = FALSE; 41 stHotKey.pfnCallBackAddr = pfnAddr; 42 stHotKey.dwVK = dwVK; 43 m_vecHotKey.push_back(stHotKey); 44 45 return TRUE; 46 } 47 48 VOID CHotKey::GetMainWindowHandle() 49 { 50 DWORD dwCurrentProcessId = GetCurrentProcessId(); 51 EnumWindows(EnumWindowsProc, (LPARAM)&dwCurrentProcessId); 52 53 } 54 55 BOOL CALLBACK CHotKey::EnumWindowsProc(HWND hwnd, LPARAM lParam) 56 { 57 DWORD dwCurProcessId = *((DWORD*)lParam); 58 DWORD dwProcessId = 0; 59 60 GetWindowThreadProcessId(hwnd, &dwProcessId); 61 if (dwProcessId == dwCurProcessId && GetParent(hwnd) == NULL) 62 { 63 m_hMainWnd = hwnd; 64 return FALSE; 65 } 66 return TRUE; 67 } 68 69 //钩子回调函数 70 LRESULT CALLBACK CHotKey::HookProc(int nCode, WPARAM wParam, LPARAM lParam) 71 { 72 vector<HOT_KEY_INFO>::iterator p = m_vecHotKey.begin(); 73 for (;p != m_vecHotKey.end(); p++) 74 { 75 if (p->bRegistered) 76 { 77 continue; 78 } 79 char szAtom[MAX_PATH] = { 0 }; 80 _snprintf_s(szAtom, sizeof(szAtom), "%s_%d", "GameAssist", p->dwVK); 81 p->atHotKey = GlobalAddAtomA(szAtom); 82 p->bRegistered = RegisterHotKey(m_hMainWnd, p->atHotKey, 0, p->dwVK); 83 } 84 85 return CallNextHookEx(m_hMsgHook, nCode, wParam, lParam); 86 } 87 88 //替换的窗口过程 89 LRESULT CALLBACK CHotKey::HookWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 90 { 91 92 if (uMsg == WM_HOTKEY) 93 { 94 vector<HOT_KEY_INFO>::iterator p = m_vecHotKey.begin(); 95 for (;p != m_vecHotKey.end(); p++) 96 { 97 if (wParam == p->atHotKey && p->pfnCallBackAddr != NULL && p->bRegistered) 98 { 99 (p->pfnCallBackAddr)(); 100 } 101 } 102 } 103 104 return CallWindowProc(m_OldWndProc, hwnd, uMsg, wParam, lParam); 105 106 }
1 #pragma once 2 #include <windows.h> 3 #include <vector> 4 5 typedef void(*VOIDFUNC)(); 6 7 struct HOT_KEY_INFO 8 { 9 DWORD dwVK; 10 VOIDFUNC pfnCallBackAddr; 11 ATOM atHotKey; 12 BOOL bRegistered; 13 }; 14 15 16 class CHotKey 17 { 18 public: 19 static BOOL SetHotKey(VOIDFUNC pfnAddr, DWORD dwVK); 20 private: 21 static VOID GetMainWindowHandle(); 22 static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam); 23 static LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam); 24 static LRESULT CALLBACK HookWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 25 static BOOL InitHotKey(); 26 static HWND m_hMainWnd; 27 static HHOOK m_hMsgHook; 28 static WNDPROC m_OldWndProc; 29 30 static std::vector<HOT_KEY_INFO> m_vecHotKey; 31 };
实现任意时候注册热键