我想请问一下,在mobile下编程可以用hook钩子函数吗?和在windows下用是一样的吗?我最近在编写一个在mobile下测试Pocket PC Device上的键盘,有的键截获不到,所以想试试用钩子函数,来截获键盘消息,各位高手在mobile下用过钩子函数吗?有的话,指点一下吧,最好是能给我一些例子借鉴一下啊
//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
HINSTANCE hInst=NULL; //引用实例的名柄
BOOL TimeInterval(LPSYSTEMTIME OldTime,LPSYSTEMTIME NowTime,int Interval);
/**
* Function Name:ActivateKBHook
*
* Function Desc:Initializes the proc. adress of various hook related APIs.
* Loads the keyboard hook.
*
* Parameters:
* HINSTANCE hInstance : handle to the application to be hooked
* HOOKPROC LLKeyboardHookCallbackFunction : procedure where the control will come to after any KB event.
* Returns:
* true if we get all the proc addresses of hook related APIs and load the hook succesfully
* false if any of the above fails
*
* Author: Prathamesh Kulkarni
**/
BOOL ActivateKBHook(HINSTANCE hInstance)
{
//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;
hInst=hInstance;
//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
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
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, KeyboardProc, hInstance, 0);
if(g_hInstalledLLKBDhook == NULL)
{
return false;
}
}
//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)
{
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)
{
return false;
}
}
//all the APIs are loaded and the application is hooked
return true;
}
//unload the coredll.dll
if(g_hHookApiDLL != NULL)
{
FreeLibrary(g_hHookApiDLL);
g_hHookApiDLL = NULL;
}
//we have terminated gracefully
return true;
}
//----------------------------------------------------------------------
// SetVideoPower - Turns on or off the display
//设定显示屏开关
int SetVideoPower (BOOL fOn) {
VIDEO_POWER_MANAGEMENT vpm;
int rc, fQueryEsc;
HDC hdc;
// Get the display dc.
hdc = GetDC (NULL);
// See if supported.
fQueryEsc = SETPOWERMANAGEMENT;
rc = ExtEscape (hdc, QUERYESCSUPPORT, sizeof (fQueryEsc),
(LPSTR)&fQueryEsc, 0, 0); //查询系统是否支持关闭屏幕
if (rc == 0) {
// No support, fail.
ReleaseDC (NULL, hdc);
return -1;
}
// Fill in the power management structure.
vpm.Length = sizeof (vpm);
vpm.DPMSVersion = 1;
if (fOn==false)
vpm.PowerState = VideoPowerOn;
else
vpm.PowerState = VideoPowerOff;
// Tell the driver to turn on or off the display.
rc = ExtEscape (hdc, SETPOWERMANAGEMENT, sizeof (vpm),
(LPSTR)&vpm, 0, 0);
// Always release what you get.
ReleaseDC (NULL, hdc);
return 0;
}