How to hook a member function of some class in C++ ?
I want to hook a member function of some class in C++, for example, Helper::func()
class Helper {
public:
void func(){
// do something here...
}
}
But I have not found related document here. Is this possible with plthook? If I want to achieve this, what efforts do I need to make? Thanks in advance.
I have not tested the following. I hope it is correct.
-
Methods defined in .cpp files Possible. The second argument of
plthook_replace()must be the corresponding mangled name.// In a header file class Helper { public: void func(); // declaration };// In .cpp file Helper::func() { // do something here... // definition } -
Methods defined in header files Impossible. The method definition may be inlined though plthook hooks only inter-module method calls.
// In a header file class Helper { public: void func() { // declaration // do something here... // definition } } -
Virtual methods Impossible. It needs another technique.
Maybe you can use a c wrapper to hook cpp function, for example.
void Foo::say_world()
{
printf("Foo::say_world\n");
}
void say_world()
{
Foo foo;
foo.say_world();
}
Is it possible to call the original c++ member function in the hooked function using the original address without knowing the type of the C++ class?