Porting C++ __fastcall Hook from x86 to x64
I have this hook function that works fine on x86. Thanks to Bo Persson.
void __fastcall Hook(Class* ThisInECX, int UnknownEDX, OtherClass* P1, void* P2, void* P3)
{
static void* OriginalFunctionPointer = GetProcAddress(GetModuleHandleA("Some.dll"), "[...]");
static auto OriginalFunction = ((void(__fastcall*)(Class* ThisInECX, int UnknownEDX, OtherClass* P1, void* P2, void* P3))OriginalFunctionPointer);
OriginalFunction(ThisInECX, UnknownEDX, P1, P2, P3);
}
Now I'm trying to port it to x64. What I found and understood from a few snippets is that: I had another variable "int UnknownRDX" at front but without it at atleast called the OriginalFunction correc开发者_开发问答tly. My real variables (P1 etc) seem to be somehow offsetted (or my problem is something different). I actually need to know if this declaration is right so I can look for the problem at even worse positions.
void Hook(Class* This, int Unknown0, int Unknown1, OtherClass* P1, void* P2, void* P3)
{
static void* OriginalFunctionPointer = GetProcAddress(GetModuleHandleA("Some64.dll"), "[...]");
static auto OriginalFunction = ((void(*)(Class* This, int Unknown0, int Unknown1, OtherClass* P1, void* P2, void* P3))OriginalFunctionPointer);
// Using P1 here is fine on x86 but not on x64
OriginalFunction(This, Unknown0, Unknown1, P1, P2, P3)
}
There is only one calling convention on x64, so you can ditch that from the signature. What's likely going wrong is that you're trying to load the x86 version of a procedure from an x64 function.
Edit: Oh wait, you posted that question about x86/x64 hooking before, right? I'm pretty confident that's not the problem here.
What I would say is that previously, your code depended on calling-convention specific hacks, but on x64 there is only one calling convention.
http://msdn.microsoft.com/en-us/library/ms235286.aspx
精彩评论