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