|
继续请教wince下的挂钩问题,兄弟们支支招啊~~~~~~~~~~~~~~~
[复制链接]
在wince下利用hook实现记录键盘某个按键的累计按键次数。
我的思路是这样的,写一个叫kbhookdll的dll安装全局钩子,在test2程序中调用。
编译也能通过,但是运行以后整个系统无法响应鼠标和键盘的任何按键了,估计是死机了。
可是在我的钩子过程函数中,根本还没有任何屏蔽操作。
- LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam)
- {
- MessageBox(NULL,_T("运行了lanuncherhook函数"),NULL,MB_OK);
- return CallNextHookEx(Hook,nCode,wParam,lParam);
- }
复制代码
运行的结果是弹出了"运行了lanuncherhook函数"对话框,但是就死机了。
我把主要的代码传上来,请大家帮帮忙看看好么?
这是dll生成文件部分:
- // kbhookdll.cpp : Defines the entry point for the DLL application.
- //
- #include "stdafx.h"
- #include "kbhookdll.h"
- #include "winceKBhook.h"
- HANDLE hMod;
- BOOL APIENTRY DllMain( HANDLE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- hMod=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;
- }
- // This is an example of an exported variable
- //KBHOOKDLL_API int nKbhookdll=0;
- // This is an example of an exported function.
- //KBHOOKDLL_API int fnKbhookdll(void)
- //{
- // return 42;
- //}
- // This is the constructor of a class that has been exported.
- // see kbhookdll.h for the class definition
- CKbhookdll::CKbhookdll()
- {
- return;
- }
- HHOOK Hook;
- LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam);
- DllExport void WINAPI InstallLaunchEv()
- {
-
- /* Hook=(HHOOK)SetWindowsHookEx(WH_KEYBOARD_LL,
- (HOOKPROC)LauncherHook,
- (HINSTANCE)hMod,
- 0);
- */
-
- if(!ActivateKBHook((HINSTANCE)hMod, LauncherHook))
- {
- MessageBox(GetActiveWindow(),
- TEXT("Couldn't intall hook...Terminating"),
- TEXT("Warning"), NULL);
- PostQuitMessage(0);
- }
- }
- LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam)
- {
- // LRESULT Result=CallNextHookEx(Hook,nCode,wParam,lParam);
-
- //if (wParam ==0x43 ) //XX为键盘对应按键
- // {
- // SendMessage(g_hwnd,WM_USER+1,0,0);
- // MessageBox(NULL,_T("成功"),NULL,MB_OK);
-
- // }
- MessageBox(NULL,_T("运行了lanuncherhook函数"),NULL,MB_OK);
-
- // return Result;
- // return 1;
- return CallNextHookEx(Hook,nCode,wParam,lParam);
- }
复制代码
运用到的一个文件winceKBhook.cpp
- #include "stdafx.h"
- #include "winceKBhook.h"
- //#include "kbhookdll.h"
- //globals
- HINSTANCE g_hHookApiDLL = NULL; //handle to coredll.dll, where all the hook related APIs are present
- HHOOK g_hInstalledLLKBDhook = NULL; //g_hInstalledLLKBDhook represents handle to the installed KB hook
- /**
- * Function Name:ActivateKBHook
- *
- * Function Desc:Initializes the proc. adress of various hook related APIs.
- * Loads the keyboard hook.
- *
- * Author: Prathamesh Kulkarni
- **/
- WINCEKBHOOK_API BOOL ActivateKBHook(HINSTANCE hInstance, HOOKPROC LLKeyboardHookCallbackFunction)
- {
- //we need to manually load these standard Win32 API calls
- //MSDN states that these aren't supported in WinCE
- SetWindowsHookEx = NULL;
- CallNextHookEx = NULL;
- UnhookWindowsHookEx = NULL;
- //now load the coredll.dll
- g_hHookApiDLL = LoadLibrary(_T("coredll.dll"));
- if(g_hHookApiDLL == NULL)
- {
- //something is awfully wrong
- //the dll has to be present
- MessageBox(NULL,TEXT("can not find coredll.dll"),TEXT("error"),MB_OK);
- return false;
- }
- else
- {
- //load the SetWindowsHookEx API call
- //the SetWindowsHookEx function installs an application-defined hook procedure into a hook chain.
- //You would install a hook procedure to monitor the system for certain types of events.
- //here we use use the hook to monitor kyeboard events
- SetWindowsHookEx = (_SetWindowsHookExW)GetProcAddress(g_hHookApiDLL, _T("SetWindowsHookExW"));
- if(SetWindowsHookEx == NULL)
- {
- //this means that MS has really stopped supporting this API in WinCE
- MessageBox(NULL,TEXT("can not find SetWindowsHookEx"),TEXT("error"),MB_OK);
- return false;
- }
- else
- {
- //install the KB hook
- //the hande needs to be saved for default processing of the events and to uninstall the hook, once we are done with it
- g_hInstalledLLKBDhook = SetWindowsHookEx(WH_KEYBOARD_LL, LLKeyboardHookCallbackFunction, hInstance, 0);
- // g_hInstalledLLKBDhook = SetWindowsHookEx(WH_KEYBOARD_LL, LauncherHook, hInstance, 0);
- if(g_hInstalledLLKBDhook == NULL)
- {
- MessageBox(NULL,TEXT("set hook error"),TEXT("error"),MB_OK);
- return false;
- }
- else MessageBox(NULL,TEXT("set hook 成功"),TEXT("error"),MB_OK);//
- }
- //load CallNextHookEx() API call
- //the CallNextHookEx function passes the hook information to the next hook procedure in the current hook chain.
- //we use this call for default processing of events.
- CallNextHookEx = (_CallNextHookEx)GetProcAddress(g_hHookApiDLL, _T("CallNextHookEx"));
- if(CallNextHookEx == NULL)
- {
- MessageBox(NULL,TEXT("can not find CallNextHookEx"),TEXT("error"),MB_OK);
- return false;
- }
- //load UnhookWindowsHookEx() API
- //the UnhookWindowsHookEx function removes a hook procedure installed in a hook chain by the SetWindowsHookEx function.
- //we use this call to unistall the hook.
- UnhookWindowsHookEx = (_UnhookWindowsHookEx)GetProcAddress(g_hHookApiDLL, _T("UnhookWindowsHookEx"));
- if(UnhookWindowsHookEx == NULL)
- {
- MessageBox(NULL,TEXT("can not find UnhookWindowsHookEx"),TEXT("error"),MB_OK);
- return false;
- }
- }
- //all the APIs are loaded and the application is hooked
- return true;
- }
- WINCEKBHOOK_API BOOL DeactivateKBHook()
- {
- //unload the hook
- if(g_hInstalledLLKBDhook != NULL)
- {
- UnhookWindowsHookEx(g_hInstalledLLKBDhook);
- g_hInstalledLLKBDhook = NULL;
- }
- //unload the coredll.dll
- if(g_hHookApiDLL != NULL)
- {
- FreeLibrary(g_hHookApiDLL);
- g_hHookApiDLL = NULL;
- }
-
- //we have terminated gracefully
- return true;
- }
复制代码
以下是winceKBhook.h
- #ifndef _WINCE_KB_HOOK_H
- #define _WINCE_KB_HOOK_H
- //used for passing to SetWindowsHookEx funtion to set a Low level (LL) keyboard hook
- #define WH_KEYBOARD_LL 20
- #define WINCEKBHOOK_API __declspec(dllexport)
- #define HC_ACTION 0
- #define WH_GETMESSAGE 3
- // Define the function types used by hooks
- typedef LRESULT (CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM lParam);
- typedef HHOOK (WINAPI *_SetWindowsHookExW)(int, HOOKPROC, HINSTANCE, DWORD);
- typedef LRESULT (WINAPI *_CallNextHookEx)(HHOOK, int, WPARAM, LPARAM);
- typedef LRESULT (WINAPI *_UnhookWindowsHookEx)(HHOOK);
- // Win32 Hook APIs
- static _SetWindowsHookExW SetWindowsHookEx;
- static _UnhookWindowsHookEx UnhookWindowsHookEx;
- static _CallNextHookEx CallNextHookEx;
- WINCEKBHOOK_API BOOL ActivateKBHook(HINSTANCE hInstance, HOOKPROC LLKeyboardHookCallbackFunction);
- WINCEKBHOOK_API BOOL DeactivateKBHook();
- #endif
复制代码
=============================================================================================================
然后是调用dll文件的对话框程序test2。在OnInitDialog()中调用InstallLaunchEv()挂钩。
|
|