// Hook.cpp : Defines the entry point for the DLL application. // #include "stdafx.h" #include "Hook.h" HHOOK g_hHook; HHOOK g_hHookKey; HINSTANCE g_hIns; #pragma data_seg("MySeg") HWND g_hWnd =NULL; #pragma data_seg() //#pragma comment(linker,"/section:MySeg,RWS") BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { g_hIns =(HINSTANCE) hModule; // switch (ul_reason_for_call) // { // case DLL_PROCESS_ATTACH: // case DLL_THREAD_ATTACH: // case DLL_THREAD_DETACH: // case DLL_PROCESS_DETACH: // break; // } return TRUE; } LRESULT CALLBACK MouseProc( int nCode, // hook code WPARAM wParam, // message identifier LPARAM lParam // mouse coordinates ) { return 1; } LRESULT CALLBACK KeyProc( int nCode, // hook code WPARAM wParam, // message identifier LPARAM lParam // mouse coordinates ) { if(wParam == VK_F2){ UnhookWindowsHookEx(g_hHook); UnhookWindowsHookEx(g_hHookKey); //MessageBox(0,"去掉了Hook","提示",0); ::SendMessage(g_hWnd,WM_CLOSE,0,0); return 1; } else return CallNextHookEx(g_hHookKey,nCode,wParam,lParam); } HOOK_API void SetHook(HWND hWnd) { g_hWnd = hWnd; // g_hHook = SetWindowsHookEx(WH_MOUSE,MouseProc,g_hIns,0); // g_hHookKey = SetWindowsHookEx(WH_KEYBOARD,KeyProc,g_hIns,0); g_hHook = SetWindowsHookEx(WH_MOUSE,MouseProc,GetModuleHandle("Hook.dll"),0); g_hHookKey = SetWindowsHookEx(WH_KEYBOARD,KeyProc,GetModuleHandle("Hook"),0); }
Hook.def
LIBRARY Hook EXPORTS SetHook @23 SEGMENTS MySeg READ WRITE SHARED
Client:
///////////////////////////////////////////////////////////////////////////// // CHookTestDlg message handlers BOOL CHookTestDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here int cx,cy; cx=GetSystemMetrics(SM_CXSCREEN); cy=GetSystemMetrics(SM_CYSCREEN); SetWindowPos(&wndTopMost,0,0,cx,cy,SWP_SHOWWINDOW); SetHook(this->m_hWnd); return TRUE; // return TRUE unless you set the focus to a control }