1

我有一个驻留在应用程序类中的函数,我的目标是将 dll 注入目标进程并通过其地址调用该成员函数。这是功能:

void GameAudio::StopAllSounds(void) // this is at address 0x004A0656

我试过这样称呼它

typedef void(__stdcall * caller)(void);
caller call = (caller)0x004A0656;

我已经使用了一切:,,,__stdcall__cdecl__thiscall名字。

4

2 回答 2

1

类的非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)();
于 2012-02-12T11:47:38.347 回答
1

C++ 中没有你正在做的事情的语法。请注意,该函数甚至可能是虚拟的,因此您将调用错误的函数。

最简单的方法是将其更改/更改为/添加非成员函数或静态成员函数,并带有附加GameAudio*参数。使用指针函数可以轻松调用该函数。

If you still want to call the member function directly your best bet is probably to use assembler. The problem then is the portability: your solution will depend on the compiler, SO, architecture and maybe even the ABI version.

于 2012-02-12T11:54:12.390 回答