使用 C++ 将非托管函数指针回调传递给托管代码互操作
我正在尝试将一些托管代码添加到现有的 C++ GUI 应用程序中。我想让一个功能回调工作......具体来说,我想将方法指针从非托管代码传递到托管代码,并让托管代码调用回调。
我正在看这样的东西:
typedef int (__stdcall *ANSWERCB)(int); //Delegate declaration
class UnmanagedObject
{
public:
UnmanagedObject() {}
int MethodD(int n) { return n; }
};
使用相应的托管类:
public delegate int CallbackDelegate(int i);
public class ManagedClass
{
public ManagedClass() {}
public void MethodD( CallbackDelegate cd ) { cd.Invoke( 5 ); }
}
问题是我一生都无法弄清楚如何从托管代码中实际调用它:
UnmanagedObject* obj = new UnmanagedObject();
ManagedLibrary::ManagedClass^ mc = gcnew ManagedLibrary::ManagedClass();
mc->MethodD( /* what do I pass here? */ );
我已经尝试过:
ManagedLibrary::CallbackDelegate^ cd = gcnew CallbackDelegate(obj, &UnmanagedObject::MethodD);
但它会生成编译器错误“委托目标需要是指向成员函数的指针”。
任何互操作专家能够提供帮助吗?
谢谢!
I'm trying to add some managed code into an existing c++ GUI application. I'd like to get a functional callback working...specifically, I'd like to pass a method pointer from UNMANAGED code to MANAGED code and have the managed code invoke the callback.
I'm looking at something like this:
typedef int (__stdcall *ANSWERCB)(int); //Delegate declaration
class UnmanagedObject
{
public:
UnmanagedObject() {}
int MethodD(int n) { return n; }
};
with a corresponding managed class:
public delegate int CallbackDelegate(int i);
public class ManagedClass
{
public ManagedClass() {}
public void MethodD( CallbackDelegate cd ) { cd.Invoke( 5 ); }
}
The problem is that I can't for the life of me figure out how to actually invoke this from managed code:
UnmanagedObject* obj = new UnmanagedObject();
ManagedLibrary::ManagedClass^ mc = gcnew ManagedLibrary::ManagedClass();
mc->MethodD( /* what do I pass here? */ );
I've tried:
ManagedLibrary::CallbackDelegate^ cd = gcnew CallbackDelegate(obj, &UnmanagedObject::MethodD);
but it generates a compiler error "delegate target needs to be a pointer to a member function".
Any interop guru's able to help out?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题始于您的 ANSWERCB 函数指针声明。它实际上不适合调用 MethodD() 函数,它是类的实例方法。首先要在本机 C++ 中实现此功能,您需要一个成员函数指针。
从那里开始,您将在托管代码中遇到更少的问题,Marshal::GetDelegateForFunctionPointer() 可以满足您的需要。
The problem starts at your ANSWERCB function pointer declaration. It isn't actually suitable for calling the MethodD() function, that's an instance method of the class. Make this work in native C++ first, you need a member function pointer.
From there, you'll have a lot less problems making it work from managed code, Marshal::GetDelegateForFunctionPointer() gets you what you need.
一半的麻烦是有问题的成员函数实际上没有与回调委托相同的签名。它本身需要一个函数指针。首先,您需要将 UnmanagedClass 更改为
我怀疑的 gcnew 调用,您只是以错误的方式使用了参数 - 即,mem func ptr,然后是 obj。
Half your trouble is that the member function in question doesn't actually have the same signature as the callback delegate. It takes a function pointer itself. First, you need to alter UnmanagedClass to be
I suspect for the gcnew call that you simply have the parameters the wrong way around- i.e., mem func ptr, then obj.