按地址调用函数

The*_*ter 2 c++ function-pointers

我试图通过地址学习一些不同的函数调用方法.

bool gl_draw_text(uint x, uint y, uint color, uint alpha, char *fmt);
Run Code Online (Sandbox Code Playgroud)

这个功能就是我所说的.以下是我现在称之为的方式.(它工作正常.)

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]
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要使​​用的方法就是这个.

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

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

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

所以要清楚,我只是要求各种方法来完成这项任务,但是在阅读了它们之后列出了一些我很好奇等等.

Oli*_*rth 6

你总是可以施放:

typedef bool (*funcptr)(uint, uint, uint, uint, char*);

funcptr draw = (funcptr)0x10057970;
Run Code Online (Sandbox Code Playgroud)

或者在C++中:

funcptr draw = reinterpret_cast<funcptr>(0x10057970);
Run Code Online (Sandbox Code Playgroud)

但是,这是完全未定义的行为.

此外,通常情况下,没有什么可以阻止编译器移动目标函数,或者甚至在它没有看到它被显式调用时完全消除它.

  • @Neil:你所有的负面评论都激励我投票.你在这吃什么? (4认同)
  • 你无意中采用了TheMonster的非常规语法,使用`(funcptr*)`而不是`(*funcptr)`.我希望你使用的编译器不是那么不同寻常地接受它! (2认同)