Class functions hook (offset)

2

I do not know the right way to explain this, but I'll try it my way. I have an .exe application and I need to create some customizations for the executable, so I created a DLL and gave the hook so that the changes are loaded. By then, everyone knows.

The scenario is this: Hook (0xOffset, & myClass :: myFunc);

There is a class in .exe that I need to completely rewrite and I've done this in my dll, but I'm having trouble 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 it did, but .exe did not find the actual address of the function.

Could you help me? Any examples?

    
asked by anonymous 06.02.2016 / 01:21

1 answer

1

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

However some compilers implement a way to accomplish this. For MSVC and GCC you can get the actual method address by doing a cast like this, assuming your method would receive an int as a parameter:

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

The magic is in type (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 get_method_addr function as follows:

Hook(0xOffset, get_method_addr(&myClass::myFunc));
    
07.02.2016 / 13:27