I use SetWindowsHookEx() to install a WH_JOURNALRECORD hook, and the function returned a HHOOK, but the hook procedure was not been called. I did every thing MSDN said, it just not work. Any help will be much gratitude~~
#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;
#define MAX_EVENT_NUM 50
static HHOOK hHookRecoInst = nullptr;
static HHOOK hHookPlayBackInst = nullptr;
static HHOOK hHookTest = nullptr;
static int recordedCount = 0;
static int playedCount = 0;
static EVENTMSG eventArry[MAX_EVENT_NUM];
static bool playBackCheck = false;
static bool recordCheck = true;
static bool SartRecordFuncCheck = false;
static bool StopRecordFuncCheck = false;
static bool PlayBackFuncCheck = false;
LRESULT CALLBACK JournalRecoProc(int code, WPARAM wParam, LPARAM lParam)
{
SartRecordFuncCheck = true;
if(code>=0)
{
switch(code)
{
case 0:
if(recordCheck)
{
EVENTMSG temp = *((PEVENTMSG)lParam);
eventArry[recordedCount++] = temp;
if(recordedCount >= MAX_EVENT_NUM)
{
UnhookWindowsHookEx(hHookRecoInst);
return 0;
}
}
break;
case 4:
recordCheck = false;
return CallNextHookEx(hHookRecoInst, code, wParam, lParam);
case 5:
recordCheck = true;
return CallNextHookEx(hHookRecoInst, code, wParam, lParam);
default:
return 0;
}
}
else
{
return CallNextHookEx(hHookRecoInst, code, wParam, lParam);
}
}
LRESULT CALLBACK JournalPlayBackProc(int code, WPARAM wParam, LPARAM lParam)
{
PlayBackFuncCheck = true;
if(code>=0)
{
switch(code)
{
case 1:
if(playedCount <= (recordedCount - 2) && playBackCheck)
{
playBackCheck = false;
*(PEVENTMSG(lParam)) = eventArry[playedCount];
if(playedCount == 0)
return 0;
else
{
return static_cast<int>(eventArry[playedCount].time - eventArry[playedCount-1].time);
}
}
else
UnhookWindowsHookEx(hHookPlayBackInst);
break;
case 2:
playBackCheck = true;
playedCount++;
break;
case 4:
playBackCheck = false;
break;
case 5:
playBackCheck = true;
break;
default:
return CallNextHookEx(hHookPlayBackInst, code, wParam, lParam);
}
}
else
return CallNextHookEx(hHookPlayBackInst, code, wParam, lParam);
}
LRESULT CALLBACK GetMsgProc(int ncode, WPARAM wParam, LPARAM lParam)
{
cout<<"GetMsgProc function called."<<endl;
return CallNextHookEx(hHookTest, ncode, wParam, lParam);
}
int _tmain(int argc, _TCHAR* argv[])
{
bool run = true;
char a;
while(run)
{
cin>>a;
if(a=='a' || a=='A')
{
hHookRecoInst = SetWindowsHookEx(WH_JOURNALRECORD, (HOOKPROC)JournalRecoProc, GetModuleHandle(nullptr), 0);
if(SartRecordFuncCheck)
{
cout<<"StartRecod funcion called."<<endl;
}
else
{
cout<<"StatRecord function not called."<<endl;
}
cout<<"Handle of this RecordHook Proc is "<<": ";
cout<<hHookRecoInst<<endl;
}
if(a=='b' || a=='B')
{
UnhookWindowsHookEx(hHookRecoInst);
}
if(a=='c' || a=='C')
{
hHookPlayBackInst = SetWindowsHookEx(WH_JOURNALPLAYBACK, (HOOKPROC)JournalPlayBackProc, GetModuleHandle(nullptr), 0);
if(PlayBackFuncCheck)
{
cout<<"PlayBack funcion called."<<endl;
}
else
{
cout<<"PlayBack function not called."<<endl;
}
cout<<"Handle of this PlayBackHook Proc is "<<": ";
cout<<hHookPlayBackInst<<endl;
}
if(a=='q' || a=='Q')
{
UnhookWindowsHookEx(hHookPlayBackInst);
run = false;
}
}
return 0;
}