简体   繁体   中英

Porting VC++ inline assembler to x64 (of a __stdcall hook)

I need to port the inline assembler to be able to compile on x64. I'm trying to get familiar with the x64 Intrinsics etc but I guess someone being into it could easily help me out.

void __stdcall Hook(P1, P2)
{
    __asm pushad

    static void* OriginalFunctionPointer =
        GetProcAddress(GetModuleHandleA("Some.dll"), "[...]");

    // [...]

    __asm popad

    __asm push (P2)
    __asm push (P1)
    __asm call (OriginalFunctionPointer)
}

seems you need a hooking library like this one (orthis if you want a C++ API) along with a function proto, then no inline assembly is needed, in 32 or 64-bit mode. also, those pushad/popad's aren't needed when you are doing inline assembly.

typedef void (__stdcall*myfp)(int,int);
void __stdcall MyHook(int arg1, int arg2)
{
    static myfp TheFP = (myfp)GetProcAddress(GetModuleHandleA("Some.dll"), "[...]");

   //your extra code
   TheFP(arg1,arg2);
}

of course the injection of this hook needs to take place somewhere else. for hooking classes you need to account for the hidden this pointer ( pDevice in this case):

#define D3D8FUNC(name,...) typedef HRESULT (__stdcall * name)(__VA_ARGS__)
D3D8FUNC(D3D8SetTexture,void* pDevice, DWORD dwStage, void* pTexture);

HRESULT __stdcall D3DSetTexture(void* pDevice, DWORD dwStage, void* pTexture)
{
    LOG("[D3DSetTexture][0x%p] Device: 0x%p Stage: %u Texture: 0x%p\n",_ReturnAddress(),pDevice,dwStage,pTexture);
    return Direct3D::gpfD3D8SetTexture(pDevice,dwStage,pTexture);
}

//in the init
Direct3D::gpfD3D8SetTexture = System::VirtualFunctionHook<Direct3D::D3D8SetTexture>(Direct3D::gpDevice,61,D3DSetTexture);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM