按地址调用函数

Invoking a function by address

本文关键字:函数 调用 地址      更新时间:2023-10-16

我正在尝试学习几种通过地址调用函数的不同方法。

bool gl_draw_text(uint x, uint y, uint color, uint alpha, char *fmt);

这个函数就是我所说的。以下是我目前对它的称呼。(它工作正常。

static void glDrawText(char* text, int x, int y)
{
DWORD func = 0x10057970;
__asm
{
    push text
    push 255
    push 14
    push y
    push x
    call dword ptr [func]
    }
}

我想使用的方法就是这个。

void Hack()
{
    bool (draw*)(uint, uint, uint, uint, char*);
    draw = 0x10057970;
    (draw)(20, 20, 14, 255, "Text");
}

但是,我不知道如何将地址正确转换为函数以使其工作\编译。

还有一种使用虚函数的方法,我也很好奇该方法是如何工作的。(我也可以使用MS Detours,钩住,然后像那样调用函数,如果你知道的话,该方法如何在幕后工作。

所以需要明确的是,我只是要求完成这项任务的各种方法,但列出了一些我在阅读后感到好奇的方法,等等。

您可以随时投射:

typedef bool (*funcptr)(uint, uint, uint, uint, char*);
funcptr draw = (funcptr)0x10057970;

或C++:

funcptr draw = reinterpret_cast<funcptr>(0x10057970);

然而,这是完全不确定的行为。

此外,一般来说,没有什么可以阻止编译器移动目标函数,甚至没有看到它被显式调用时完全消除它。

编译此代码(参见 http://ideone.com/celq1):

typedef unsigned int uint ;
int main()
{
    bool (*draw)(uint, uint, uint, uint, const char*);
    draw = reinterpret_cast<bool (*)(uint, uint, uint, uint, const char*)>(0x10057970);
    draw(20, 20, 14, 255, "Text");
}

但当然它不会运行:-)
PS 我将char*更改为 const char* 以摆脱编译器警告。看起来const char*是你想要的,但它对这个想法并不重要。

编辑添加:事实上,即使这样编译,如果你想给你的朋友留下深刻印象:

typedef unsigned int uint ;
int main()
{
    reinterpret_cast<bool (*)(uint, uint, uint, uint, const char*)>(0x10057970)
      (20, 20, 14, 255, "Text");
}