KeyHookApp.cpp
1 //////////////////////////////////////////////// 2 // KeyHookApp.cpp文件 3 4 #include "resource.h" 5 #include "KeyHookApp.h" 6 #include "../09KeyHookLib/KeyHookLib.h" 7 8 #pragma comment(lib, "09KeyHookLib") 9 10 CMyApp theApp; 11 12 BOOL CMyApp::InitInstance() 13 { 14 CMainDialog dlg; 15 m_pMainWnd = &dlg; 16 dlg.DoModal(); 17 return FALSE; 18 } 19 20 CMainDialog::CMainDialog(CWnd* pParentWnd):CDialog(IDD_MAIN, pParentWnd) 21 { 22 } 23 24 BEGIN_MESSAGE_MAP(CMainDialog, CDialog) 25 ON_MESSAGE(HM_KEY, OnHookKey) 26 END_MESSAGE_MAP() 27 28 BOOL CMainDialog::OnInitDialog() 29 { 30 CDialog::OnInitDialog(); 31 SetIcon(theApp.LoadIcon(IDI_MAIN), FALSE); 32 ::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 33 0, 0, SWP_NOSIZE|SWP_NOREDRAW|SWP_NOMOVE); 34 35 // 安装钩子 36 if(!SetKeyHook(TRUE, 0, m_hWnd)) 37 MessageBox("安装钩子失败!"); 38 39 return TRUE; 40 } 41 42 43 void CMainDialog::OnCancel() 44 { 45 // 卸载钩子 46 SetKeyHook(FALSE); 47 CDialog::OnCancel(); 48 return; 49 } 50 51 long CMainDialog::OnHookKey(WPARAM wParam, LPARAM lParam) 52 { 53 // 此时参数wParam为用户按键的虚拟键码, 54 // lParam参数包含按键的重复次数、扫描码、前一个按键状态等信息 55 56 char szKey[80]; 57 ::GetKeyNameText(lParam, szKey, 80); 58 59 CString strItem; 60 strItem.Format(" 用户按键:%s \r\n", szKey); 61 // 添加到编辑框中 62 CString strEdit; 63 GetDlgItem(IDC_KEYMSG)->GetWindowText(strEdit); 64 GetDlgItem(IDC_KEYMSG)->SetWindowText(strItem + strEdit); 65 66 ::MessageBeep(MB_OK); 67 return 0; 68 }
MainDir.h文件
1 /////////////////////////////////////////// 2 // MainDir.h文件 // 07MainDir 3 4 #include <afxwin.h> 5 6 7 class CMyApp : public CWinApp 8 { 9 public: 10 BOOL InitInstance(); 11 }; 12 13 class CMainDialog : public CDialog 14 { 15 public: 16 CMainDialog(CWnd* pParentWnd = NULL); 17 18 protected: 19 virtual BOOL OnInitDialog(); 20 virtual void OnCancel(); 21 22 afx_msg long OnHookKey(WPARAM wParam, LPARAM lParam); 23 24 DECLARE_MESSAGE_MAP() 25 };
resource.h
1 //{{NO_DEPENDENCIES}} 2 // Microsoft Developer Studio generated include file. 3 // Used by KeyHookApp.rc 4 // 5 #define IDD_MAIN 101 6 #define IDI_MAIN 103 7 #define IDC_KEYMSG 1000 8 9 // Next default values for new objects 10 // 11 #ifdef APSTUDIO_INVOKED 12 #ifndef APSTUDIO_READONLY_SYMBOLS 13 #define _APS_NEXT_RESOURCE_VALUE 104 14 #define _APS_NEXT_COMMAND_VALUE 40001 15 #define _APS_NEXT_CONTROL_VALUE 1001 16 #define _APS_NEXT_SYMED_VALUE 101 17 #endif 18 #endif
上面是app工程中包含的代码
下面是dll对应的代码
KeyHookLib.cpp文件
1 //////////////////////////////////////////////// 2 // KeyHookLib.cpp文件 3 4 #include <windows.h> 5 6 #define KEYHOOKLIB_EXPORTS 7 #include "KeyHookLib.h" 8 9 10 // 共享数据段 11 #pragma data_seg("YCIShared") 12 HWND g_hWndCaller = NULL; // 保存主窗口句柄 13 HHOOK g_hHook = NULL; // 保存钩子句柄 14 #pragma data_seg() 15 16 // 一个通过内存地址取得模块句柄的帮助函数 17 HMODULE WINAPI ModuleFromAddress(PVOID pv) 18 { 19 MEMORY_BASIC_INFORMATION mbi; 20 if(::VirtualQuery(pv, &mbi, sizeof(mbi)) != 0) 21 { 22 return (HMODULE)mbi.AllocationBase; 23 } 24 else 25 { 26 return NULL; 27 } 28 } 29 30 // 键盘钩子函数 31 LRESULT CALLBACK KeyHookProc(int nCode, WPARAM wParam, LPARAM lParam) 32 { 33 if(nCode < 0 || nCode == HC_NOREMOVE) 34 return ::CallNextHookEx(g_hHook, nCode, wParam, lParam); 35 36 if(lParam & 0x40000000) // 消息重复就交给下一个hook链 37 { 38 return ::CallNextHookEx(g_hHook, nCode, wParam, lParam); 39 } 40 41 // 通知主窗口。wParam参数为虚拟键码, lParam参数包含了此键的信息 42 ::PostMessage(g_hWndCaller, HM_KEY, wParam, lParam); 43 44 return ::CallNextHookEx(g_hHook, nCode, wParam, lParam); 45 } 46 47 // 安装、卸载钩子的函数 48 BOOL WINAPI SetKeyHook(BOOL bInstall, DWORD dwThreadId, HWND hWndCaller) 49 { 50 BOOL bOk; 51 g_hWndCaller = hWndCaller; 52 53 if(bInstall) 54 { 55 g_hHook = ::SetWindowsHookEx(WH_KEYBOARD, KeyHookProc, 56 ModuleFromAddress(KeyHookProc), dwThreadId); 57 bOk = (g_hHook != NULL); 58 } 59 else 60 { 61 bOk = ::UnhookWindowsHookEx(g_hHook); 62 g_hHook = NULL; 63 } 64 65 return bOk; 66 }
KeyHookLib.h文件
1 /////////////////////////////////////////// 2 // KeyHookLib.h文件 3 4 // 定义函数修饰宏,方便引用本DLL工程的导出函数 5 #ifdef KEYHOOKLIB_EXPORTS 6 #define KEYHOOKLIB_API __declspec(dllexport) 7 #else 8 #define KEYHOOKLIB_API __declspec(dllimport) 9 #endif 10 11 // 自定义与主程序通信的消息 12 #define HM_KEY WM_USER + 101 13 14 // 声明要导出的函数 15 BOOL KEYHOOKLIB_API WINAPI SetKeyHook(BOOL bInstall, 16 DWORD dwThreadId = 0, HWND hWndCaller = NULL);
同时应还包含***.def文件