C++成员函数作为外部库的回调函数

C++ member function as callback function to external library

本文关键字:函数 外部 回调 成员 C++      更新时间:2023-10-16

下面是我尝试做什么的基本想法。我有一个外部库,我想在现有项目中使用它。当然,我不能更改外部库中的任何内容或现有项目中的主要功能。

我面临的问题是如何将我在类中创建的回调函数作为指向函数的指针传递给这个外部函数。同时,这个回调函数必须能够访问类的成员,所以我不能简单地使它成为静态的。我该怎么做?

Class ExternalClass //This I cannot mess with.
{
    //somestuff
    void ExternalFunc (void(* callback)(int, const void*), const void *);
}
Class MyClass
{
    //somestuff
    ExternalClass m_ExObj;
    void Callback(int x, const void *p){
        //dosomething
        //How do I use this pointer ?
    }
    void MyFunc(){
        m_ExObj.ExternalFunc(/*Some way to put MyClass::Callback() in here*/)
    }
}

您显示的回调不允许将用户定义的值传递给它(否则您可以使用它来传递对象指针)。它需要一个独立的非类函数,因此有两个选项:

1) 如果回调一次只调用一个对象,那么您可以将对象指针存储在全局或static变量中,然后使用独立函数(或static类方法)作为回调,并让它使用全局/静态指针来调用您的类方法:

class MyClass
{
    //somestuff
    ExternalClass m_ExObj;
    void Callback(int x)
    {
        //dosomething
    }
    static MyClass* objForCallback;
    static void exObjCallback(int x) { objForCallback->Callback(x); }
    void MyFunc()
    {
        objForCallback = this;
        m_ExObj.ExternalFunc(&exObjCallback);
    }
};

2) 如果需要一次对多个对象进行回调,则必须将类方法封装在每个对象的thunk中,其中每个thunk都知道要调用哪个对象,然后将thunk用作回调。这是一种更高级的技术,需要了解x86/x64程序集和调用约定,因为您必须动态分配内存,并用程序集指令填充内存,以便每个thunk在运行时执行。例如,至少在Windows 32位上:

#pragma pack(push, 1)
struct MyThunk
{
    unsigned char PopEAX_1;     // POP the caller's return address off the stack
    unsigned char PushThis;     // PUSH the object 'this' pointer on to the stack
    void *ThisValue;
    unsigned char PushEAX_1;    // PUSH the caller's return address back on to the stack
    unsigned char Call;         // CALL the callback function
    __int32 CallAddr;
    unsigned char PopEAX_2;     // POP the caller's return address off the stack
    unsigned char AddESP[3];    // Remove the object 'this' pointer from the stack
    unsigned char PushEAX_2;    // PUSH the caller's return address back on to the stack
    unsigned char Return;       // return to the caller
};
#pragma pack(pop)
typedef void (*CallbackType)(int);
class MyClass
{
    CallbackType exObjCallback;
    MyClass()
    {
        MyThunk *thunk = (MyThunk*) VirtualAlloc(NULL, sizeof(MyThunk), MEM_COMMIT, PAGE_READWRITE);
        if (thunk)
        {
            thunk->PopEAX_1 = 0x58;
            thunk->PushThis = 0x68;
            thunk->ThisValue = this;
            thunk->PushEAX_1 = 0x50;
            thunk->Call = 0xE8;
            thunk->CallAddr = reinterpret_cast<__int32>(Callback) - (reinterpret_cast<__int32>(&thunk->Call) + 5);
            thunk->PopEAX_2 = 0x58;
            thunk->AddESP[0] = 0x83;
            thunk->AddESP[1] = 0xC4;
            thunk->AddESP[2] = 0x04;
            thunk->PushEAX_2 = 0x50;
            thunk->Return = 0xC3;
            DWORD dwOldProtect;
            VirtualProtect(thunk, sizeof(MyThunk), PAGE_EXECUTE, &dwOldProtect);
            FlushInstructionCache(GetCurrentProcess(), thunk, sizeof(MyThunk));
            exObjCallback = (CallbackType) thunk;
        }
    }
    ~MyClass()
    {
        if (exObjCallback)
            VirtualFree(exObjCallback, 0, MEM_RELEASE);
    }
    //somestuff
    ExternalClass m_ExObj;
    // NOTE: pCtx is the return address inside of ExternalFunc()
    // where the callback is being called from.  Because the
    // callback is using the __cdecl calling convention, the
    // thunk needs to remember this value and restore it after
    // Callback() exits.  Passing it as a parameter to Callback()
    // is a quick-n-dirty way for the thunk to do that...
    static void __cdecl Callback(void *pCtx, MyClass *pThis, int x)
    {
        //dosomething with pThis
    }
    void MyFunc()
    {
        if (exObjCallback)
            m_ExObj.ExternalFunc(exObjCallback, ...);
    }
};

ExternalFunc()调用它的回调时,它将调用thunk,执行它包含的指令。上面的thunk将对象的this指针作为Callback()的参数注入到调用堆栈中,就好像ExternalFunc()直接调用了它一样。

更新:代替回调实际接受用户定义值的新信息,这大大简化了事情:

class MyClass
{
    //somestuff
    ExternalClass m_ExObj;
    static void Callback(int x, const void *p) {
        MyClass *pThis = (MyClass*) p;
        //dosomething with pThis
    }
    void MyFunc() {
        m_ExObj.ExternalFunc(&Callback, this);
    }
};