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

时间:2015-06-24 05:02:50

标签: c++ callback member member-function-pointers

以下是我正在尝试做的基本想法。 我有一个外部库,我想在现有项目中使用。 当然,我无法在外部库或现有项目中的主要功能中进行任何更改。

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

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 个答案:

答案 0 :(得分:4)

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

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

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 32bit上:

#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);
    }
};