全局键盘钩子问题
今天写了一个全局键盘钩子,但是在别的程序里面打字,这个钩子失灵了,问问原因
下面是DLL文件的代码
#include "hookkey.h"#pragma code_seg("code")HWND phmain = NULL ;HHOOK g_keyhook = NULL ;HHOOK g_mousehook = NULL ;#pragma code_seg()#pragma comment(linker , "/SECTION:code,RWS")BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ){// 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 KeyboardProc( int code, // hook code WPARAM wParam, // virtual-key code LPARAM lParam // keystroke-message information ){ if (code <0 ||code== HC_NOREMOVE ) { return CallNextHookEx(g_keyhook , code , wParam ,lParam); } if (code & 0x40000000) //消息重复就交给下一个HOOK链 { return CallNextHookEx(g_keyhook , code , wParam , lParam ); } PostMessage( phmain , WM_KEY , wParam , lParam ); return CallNextHookEx(g_keyhook , code , wParam,lParam );}LRESULT CALLBACK MouseProc( int nCode, // hook code WPARAM wParam, // message identifier LPARAM lParam // mouse coordinates ){ return 1;}HOOKKEY_API void sethook(int instal , HWND mhwnd ){ if (instal == 1) { phmain = mhwnd ; g_keyhook =SetWindowsHookEx(WH_KEYBOARD ,KeyboardProc , GetModuleHandle("hookkey") , 0 ); } else { UnhookWindowsHookEx(g_keyhook); }}HOOKKEY_API void setsss(int instal , HWND mhwnd ){ if (instal == 1) { phmain = mhwnd ; g_mousehook =SetWindowsHookEx(WH_MOUSE ,MouseProc , GetModuleHandle("hookkey") , 0 ); } else { UnhookWindowsHookEx(g_mousehook); }}
char szkey[80]; GetKeyNameText(lParam, szkey , 80); m_code += szkey ; m_code += "\r\n" ; UpdateData(false);