fywdm
April 6th, 2012, 18:03
Hi,
I'm using the MS Detours library for hooking and I've read about several ways to hook C++ __thiscall subroutines (e.g. member functions of classes). For example, using __stdcall, __fastcall and __declspec(naked).
I've got pretty good results by using __declspec(naked). For example, the following detour subroutine works well.
Unfortunately, it has some limitations which have been described at http://msdn.microsoft.com/en-us/library/4d12973a(v=vs.80).aspx.
Whenever I use, __stdcall it causes ecx to become 0 (the this pointer is not being passed). So __stdcall seems to be out of the question in this case. Unless I'm missing something here...
I've tried using __fastcall. However, it eventually leads to crashes. For example, the following piece of of code does not work properly.
I've omitted "_asm ecx, eax" due to ecx being passed.
What's recommended method for hooking class member functions?
Thanks in advance,
FY
I'm using the MS Detours library for hooking and I've read about several ways to hook C++ __thiscall subroutines (e.g. member functions of classes). For example, using __stdcall, __fastcall and __declspec(naked).
I've got pretty good results by using __declspec(naked). For example, the following detour subroutine works well.
Code:
__declspec( naked ) int foo(int pThis, int bar){
* * * * _asm push 1
* * * * _asm mov ecx, eax
* * * * _asm call originalSubroutine
* * * * _asm retn 4
}
Unfortunately, it has some limitations which have been described at http://msdn.microsoft.com/en-us/library/4d12973a(v=vs.80).aspx.
Whenever I use, __stdcall it causes ecx to become 0 (the this pointer is not being passed). So __stdcall seems to be out of the question in this case. Unless I'm missing something here...

I've tried using __fastcall. However, it eventually leads to crashes. For example, the following piece of of code does not work properly.
Code:
__fastcall int foo(int pThis, int bar){
* * * * _asm push 1
* * * * _asm call originalSubroutine
* * * * _asm retn 4
}
I've omitted "_asm ecx, eax" due to ecx being passed.
What's recommended method for hooking class member functions?
Thanks in advance,
FY