Class functions hook (offset)

Asked

Viewed 265 times

2

I don’t know the right way to explain this question, but I’ll try it my way. I have an app. exe and I need to create some customizations for the executable, so I created a DLL and gave hook for the changes to be loaded. Until then, everyone knows.

This is the scenario: Hook(0xOffset, &myclass::myFunc);

There’s a class in the. exe that I need to rewrite completely and I’ve already done this in my dll, but I’m having problems with the hook in the class functions, they are not static. I read many topics and I could not implement with the methods that were presented by other people. In some cases, the compiler did not accept, in others accepted, but . exe did not find the actual address of the function.

Could you help me? Some example?

1 answer

1


If your problem is getting the address of a method directly like &myClass::myFunc, well, by default language this is not possible, as it depends on the compiler’s implementation for virtual tables (VTABLE), a pointer to a method is a special type of pointer, which may contain the VTABLE address along with other information, then use the operator & to get the method address can return only the table and some index to it and then when you access with myObj->*myFunc(...), compiler will know which method to call.

But some compilers implement a way to achieve this. For MSVC and GCC you can get the actual address of the method by casting a cast like this, assuming your method received an int as parameter:

size_t get_method_addr(void (myClass::*f)(int)) {
    return reinterpret_cast<size_t>((void* &) f);
}

The magic is in the guy (void* &). The problem with this is that for each method that receives parameters and returns different types it will be necessary to rewrite a function like this. For our luck using C++11 with variadic templates we can implement a generic function that deduces all types and returns the address:

template<class Class, class ReturnType, typename...Args>
size_t get_method_addr(ReturnType (Class::*f)(Args...)) {
    return reinterpret_cast<size_t>((void* &)f);
}

Now you can use the function get_method_addr as follows:

Hook(0xOffset, get_method_addr(&myClass::myFunc));
  • I don’t even know how to thank you. You saved my life! I’ve been trying for days to do something and I can’t. Thank you so much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.