按地址调用函数
Invoking a function by address
我正在尝试学习几种通过地址调用函数的不同方法。
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");
}
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销