简体   繁体   中英

Pointer to member class

I have a hooking library that i am porting to C++, the only problem i have its that i need a void pointer to client function, and i cant get a raw pointer of any function member, so for now it only works with static functions. This enforce me to make a singleton pattern which i dont want to.

Here is a snippet of the current problem i am facing for making an class that prints data regarding functions that are hooked:

class SpyLib
{
private:
    HookLib hooklib;
    std::vector<SpyRecord> records;

    SpyRecord findRecord(int id);
public:
    //Member function to callback after the hook
    void spyer(void* register_context, int hookid);
};

Since i cant get the address of spyer function i need to declare it "static" that makes me declare the findRecord as well for finding data regarding the hook process. Since findRecord use "records" vector, that needs to be static too and so on. In the end i am dealing with a static class and forced to use a singleton pattern.

Question is: is there any method besides messing with vtables for finding the VA of a member function? if not, then how detour library from microsoft does it?.

Thanks.

You can always bypass the problem by delegating function call to wrapper non-member function:

class Pars
{
public:
SpyLib* spyLibPtr;
void* register_context;
int hookid;
};

void call_spyer(void* voidPtr)
{
   Pars* parsPtr = reinterpret_cast<Pars*>(voidPtr);
   parsPtr->spyLibPtr->spyer(parsPtr->register_context,parsPtr->hookid);
}

This way you can pass around pointer to call_spyer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM