#pragma once #include <Windows.h> class recorderplay { public: recorderplay(void); virtual ~recorderplay(void); void recorder(const char*); void play(const char*); }; #include "StdAfx.h" #include "recorderplay.h" EVENTMSG esg; DWORD basetime; HHOOK rphook; extern CListBox* plistbox; CString strmsg; ifstream in; ofstream out; void unhook(void); LRESULT CALLBACK JournalRecordProc( int code, // hook code WPARAM wParam, // not used LPARAM lParam // message being processed ) { int pos; if (code<0) return CallNextHookEx(rphook,code,wParam,lParam); if (code==HC_ACTION) { esg=*(PEVENTMSG)lParam; esg.time=esg.time-basetime; out.write((char*)&esg,sizeof(esg)); strmsg.Format(L"%d %d %d pos:%d",esg.message,esg.paramL,esg.time,pos=out.tellp()); plistbox->AddString(strmsg); if ((esg.message>WM_KEYFIRST)&&(esg.message<WM_KEYLAST)&&(LOBYTE(esg.paramL)==VK_F12)) { strmsg.Format(L"%d",pos=out.tellp()); out.close();plistbox->AddString(L"stop pos:"+strmsg);unhook(); } } return CallNextHookEx(rphook,code,wParam,lParam); } LRESULT CALLBACK JournalPlaybackProc( int code, // hook code WPARAM wParam, // not used LPARAM lParam // message being processed ) { int pos; if (code<0) return CallNextHookEx(rphook,code,wParam,lParam); if (code==HC_SKIP) { in.read((char*)&esg,sizeof(esg)); if (pos=in.tellg()<0) {plistbox->AddString(L"eof");in.close();unhook();return 0;} strmsg.Format(L"%d %d %d pos:%d",esg.message,esg.paramL,esg.time,pos=in.tellg()); plistbox->AddString(strmsg); esg.time=basetime+esg.time; } if (code==HC_GETNEXT) { if (esg.time-GetTickCount()<0) { *(PEVENTMSG(lParam))=esg; return 0; } else { *(PEVENTMSG(lParam))=esg; return esg.time-GetTickCount(); } } return CallNextHookEx(rphook,code,wParam,lParam); } void unhook(void) { if (rphook) UnhookWindowsHookEx(rphook); MessageBoxA(NULL,"unhookwindowshook finished","record and playback",MB_OK); } recorderplay::recorderplay(void) { } recorderplay::~recorderplay(void) { } void recorderplay::recorder(const char*a) { out.open(a,ios::binary|ios::out); rphook=SetWindowsHookEx(WH_JOURNALRECORD,JournalRecordProc,GetModuleHandle(NULL),0); basetime=GetTickCount(); } void recorderplay::play(const char*a) { in.open(a,ios::binary); rphook=SetWindowsHookEx(WH_JOURNALPLAYBACK,JournalPlaybackProc,GetModuleHandle(NULL),0); basetime=GetTickCount(); }