Notifying Managed Code of a Native Callback
Here’s a way for unmanaged code to notify managed code of a callback. The two code types use different calling conventions and the user cannot change the latter’s convention. Instead, use an inner class like this:
__gc classManagedClass
{
private:
__nogc class UnmanagedClass
{
static void __cdecl MyCallback()
{
ManagedClass *mc = ManagedClass::GetClass();
mc->MyFunc();
}
};
ManagedClass *thisClass;
public:
ManagedClass() { thisClass = this; }
static ManagedClass* GetClass() { return thisClass; }
void MyFunc() {}
};
The GetClass() method is a bit unsightly, but elegent nonetheless. The managed code can pass MyCallback to unmanaged code via a function pointer. The unmanaged code calls MyCallback and then MyCallback calls the managed method MyFunc(). This approach is especially useful for IJW DLLs in VC7.
References:
Leave a Reply
You must be logged in to post a comment.