GuiTeK
GuiTeK

Reputation: 1731

Hooking non-virtual class member function

I managed to hook everything I wanted (API functions, virtual methods, "standard" functions) but I'm wondering how to hook a non-virtual class member function (method). To hook a virtual method you just need to get the VTable and patch it (or copy it, modify it and change the VPointer). However, when the method is NOT virtual, there is no VTable.

First of all, how can I get the address of the method I want to hook from its name? I can't use GetProcAddress() since the function is not exported. The only way seems to search for a byte pattern in memory corresponding to the function. Then, once I got the address, how do I hook it? Using a basic method (JMP)? What if I want to hook only ONE instance? I think I'd do check in my hooking function: if it's the right instance then do what has to be done, otherwise just execute it without doing anything else.

Actually I'm looking for better solutions because I think the ones above would work but they're not very "neat", are they?

Thank you.

Upvotes: 0

Views: 1092

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283713

Most of the time it is simply not possible to divert a statically-resolved function. Have you ever heard of this optimization called inlining? Even when that doesn't occur, COMDAT-folding can make isolating your function of interest quite impossible.

Strongly recommended reading: Raymond Chen's "Why does the debugger show me the wrong function?"

Upvotes: 1

Related Questions