首页 > 解决方案 > 挂钩函数

问题描述

我正在使用 VS2019,我想在加载到进程中的模块中挂钩函数这是函数代码

void C::GetDrawPosition(float* pX, float* pY, bool* pbBehindCamera, QAngle angleCrosshairOffset)
{
    ...

    * pX = x;
    * pY = y;
    *pbBehindCamera = bBehindCamera;
}

...

class C
{
public:
       static void  GetDrawPosition ( float *pX, float *pY, bool *pbBehindCamera, QAngle angleCrosshairOffset = vec3_angle );
};

这是函数原型以及我如何挂钩它

typedef void(__cdecl *GetDrawPosition_t) (float*, float*, bool*, QAngle);
extern GetDrawPosition_t GetDrawPosition_s;

...

GetDrawPosition_t GetDrawPosition_s = nullptr;

DWORD GetDrawPosition_adr = GetClientSig("55 8B EC ...");
XASSERT(GetDrawPosition_adr);

GetDrawPosition_s = (GetDrawPosition_t)DetourFunction((LPBYTE)GetDrawPosition_adr,(LPBYTE)&Hooked_GetDrawPosition);

和我的钩子

void __cdecl Hooked_GetDrawPosition(float* pX, float* pY, bool* pbBehindCamera, QAngle angleCrosshairOffset)
{
    *pX = *pX - 400;
    *pY = *pY - 101;
    
    GetDrawPosition_s(pX, pY, pbBehindCamera, angleCrosshairOffset);
}

问题是它没有做任何改变,我尝试使用不同的约定类型,我不知道我做错了什么,签名是正确的 100%

提前感谢您的帮助

标签: c++

解决方案


如果C是类且GetDrawPosition不是静态成员函数,则类型C::GetDrawPosition(float* pX, float* pY, bool* pbBehindCamera, QAngle angleCrosshairOffset)is not void(__cdecl *GetDrawPosition_t) (float*, float*, bool*, QAngle),其类型为

 void(__cdecl C::*GetDrawPosition_t) (float*, float*, bool*, QAngle)

它期望您必须以C某种方式传递指向类型对象实例的指针。

你不能直接绕过非静态方法,至少在 Detour 1.0 中是这样。我看到了 2.0\ 3.0 Express 的例子(你可以在网上找到)


推荐阅读