我有一个驻留在应用程序类中的函数,我的目标是将一个 dll 注入(inject)目标进程并通过其地址调用该成员函数。 这是函数:
void GameAudio::StopAllSounds(void) // this is at address 0x004A0656
我试过这样调用它
typedef void(__stdcall * caller)(void);
caller call = (caller)0x004A0656;
我已经使用了所有东西:__stdcall
、__cdecl
、__thiscall
随便你说。
最佳答案
类的非static
成员函数签名与普通函数不同。
例如,
class X {
public:
void foo ();
};
void foo ();
在上面的例子中,X::foo()
和 ::foo()
彼此不同。请记住,引擎盖下的 X::foo()
有点像:
void X::foo (X* const this);
^^^^^ implicitly added
因此,您尝试执行的操作会导致未定义的行为。
如果您坚持使用其地址调用成员函数,那么最好将其设为static
成员函数。
class X {
public:
static void foo ();
};
void foo ();
X::foo()
和 ::foo()
的签名相同。
编辑:从您的评论来看,您似乎无法控制源代码。我建议对函数签名使用正确的调用约定而不是硬编码地址。伪代码
typedef void (GameAudio::*caller_type)();
caller_type caller = &GameAudio::StopAllSounds;
GameAudio object;
(object.*caller)();
关于c++ - 如何从他们的地址调用成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9248496/