Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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~~
C++
#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;


////////////hook mian//////////////
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 CallNextHookEx(hHookRecoInst, code, wParam, lParam);
			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);
}


/////////mian///////////

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;
}
Posted

HHOOK and other global variables that need to be shared among all the processes must be declared in a shared data segment.
static is not sufficient for this.
Shared data segments can be created as explained here - #pragma data_seg[^]
I have some code in this article that I wrote a long time ago - Mousey! Roll Over and Park[^]
 
Share this answer
 
Comments
Kydz_Leo 12-May-12 1:09am    
Firstly, thanks for the solution, I created a data segment and declared all the global variables in it, but still not working, according to MSDN journal hooks do not need to live in a DLL, so I think the declarations are OK, the problem is why the hook procedure is not been called...
Well you need to check your return value to SetWindowHookEx, and perhaps use GetLastError and lookup the error code right after calling SetWindowHookEx.



Return value
Type:

Type: HHOOK


If the function succeeds, the return value is the handle to the hook procedure.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.
 
Share this answer
 
Comments
Kydz_Leo 12-May-12 23:12pm    
Thanks for reply, I`ve done the check and there is a return value. And i also called GetLastError, but there is not any errors which probably means that SetWindowsHookEx works OK, I think that is the problem of CALLBACK function not been called.
jkirkerx 13-May-12 18:32pm    
If it works correctly, the callback will start running, at least make it to the first line of the callback,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900