I have a jump from original function to my hook, which runs assembly that executes a function. I'm trying to pass arguments from the original function to the function mWSARecv.
Here's how I do it:
void mWSARecv(LPWSABUF lpBuffers)
{
std::cout << "WSARecv: " << lpBuffers->buf << " Len: " << lpBuffers->len << std::endl;
}
__declspec(naked) int hookWSARecv() // Original -> Here
{
__asm
{
pushad;
pushfd;
push[ebp + 0x24];
call mWSARecv;
popfd;
popad;
jmp WSARecvTramp;
}
}
I then save registers and flags. Push the desired argument [ebp + 0x24] and call the function which outputs those. It works once, but the next time it causes an execption.
The original function calling convention is __stdcall.
First jump:
Assembly hook:
What am I doing wrong?


mWSARecvis CDECL calling convention the caller has to cleanup the stack. You push 4 bytes withpush[ebp + 0x24];so you'd have to add add 4 to esp after. Maybe placeadd esp, 4after thecall mWSARecv- Michael PetchmWSARecvis CDECL the 4 bytes pushed by push[ebp + 0x24];` have to be cleaned up by you (with something likeadd esp, 4). Failure to do the add of 4 to ESP (in this case) means when mWSARecv returns it will then executepopfdandpopadbut everything is shifted by 4 on the stack meaning everything that restored by those 2 instructions is corrupt. - Michael Petch