Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to hook a member function of some class in C++ ? #31

Open
zsnjuts opened this issue Feb 4, 2021 · 2 comments
Open

How to hook a member function of some class in C++ ? #31

zsnjuts opened this issue Feb 4, 2021 · 2 comments

Comments

@zsnjuts
Copy link

zsnjuts commented Feb 4, 2021

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.

@kubo
Copy link
Owner

kubo commented Feb 6, 2021

I have not tested the following. I hope it is correct.

  1. 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
    }
  2. 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
        }
    }
  3. Virtual methods
    Impossible. It needs another technique.

@forunix
Copy link

forunix commented Apr 2, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants