C++成员函数作为外部库的回调函数
C++ member function as callback function to external library
下面是我尝试做什么的基本想法。我有一个外部库,我想在现有项目中使用它。当然,我不能更改外部库中的任何内容或现有项目中的主要功能。
我面临的问题是如何将我在类中创建的回调函数作为指向函数的指针传递给这个外部函数。同时,这个回调函数必须能够访问类的成员,所以我不能简单地使它成为静态的。我该怎么做?
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);
}
};
相关文章:
- 在函数内创建的对象的范围 - 如果在函数外部存储和访问引用,它们是否有效?
- C++指针无法在函数外部传递值和分段错误(核心转储)错误
- C++:函数外部的超时功能
- 如何在函数外部访问函数中局部变量的值?
- 在成员函数外部封闭类的定义中需要默认成员初始值设定项
- 以延长构造函数外部 QT 对象的生存期
- 全局静态变量不"staying defined"函数外部
- C++ 函数外部丢失的指针对象
- 向量大小在构造函数外部恢复为 0
- 删除函数外部的实例化对象
- 在函数外部编写代码
- 为什么我不能添加到方法/函数外部的 ostringstream
- 用于在方法或函数外部声明的变量的内存
- SDL2 在函数外部加载字体会引发错误
- 如何在具有多态性的函数外部创建对象
- 在函数外部声明对象,将其作为引用传递,然后在函数内部创建它
- 向量未在函数C++外部更新
- 访问函数外部的堆栈对象
- 在c++中访问函数外部的数组
- 如何更新函数外部的数组(指向指针)