Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hi !

I have a little function which is used to hook virtual functions under Windows x86.
I have to port it or linux x86, I know I have to use mprotect but this is abstract for me, I've searched all the night on google, here and on other site but ... wtf ... no answer at all ?? Linux doesn't exists or what ??

C++
DWORD VirtualTableHook( DWORD* pdwNewInterface, int vtable, DWORD newInterface )
{
	DWORD dwOld, dwStor = 0x0;
#ifdef WIN32
	VirtualProtect( &pdwNewInterface[vtable], 4, PAGE_EXECUTE_READWRITE, &dwOld );
#else

#endif
	dwStor = pdwNewInterface[vtable];
	*(DWORD*)&pdwNewInterface[vtable] = newInterface;
#ifdef WIN32
	VirtualProtect(&pdwNewInterface[vtable], 4, dwOld, &dwOld);
#else

#endif
	return dwStor;
}


I don't know if I can use DWORD under Linux, I don't know how to use mprotect exactly, even if I take a look at man ... so if someone could help me it will be much appreciated. Thanks :o
Posted
Comments
pasztorpisti 5-Nov-12 4:30am    
Please note that the vtable contains function pointers that is 64 bits on a 64 bit system so using a DWORD isn't correct not even on a 64 bit windows (DWORD_PTR would be better that is 32 bits on x86 64 bits on x64). I would create my own define for the function pointer type (like typedef void* PFunc) and I would switch to (void*) instead of DWORD.

Try this sample[^]. I do not think that DWORD is defined in any of the Linux C/C++ headers, but you can add it yourself quite easily.
 
Share this answer
 
DWORD on x86 is a 32 bit unsigned integer. Is easy to find definition. Put cursor on DWORD in VisualStudio and press F12.
 
Share this answer
 

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