使用 C++ 将非托管函数指针回调传递给托管代码互操作

发布于 2024-09-16 11:46:22 字数 955 浏览 7 评论 0原文

我正在尝试将一些托管代码添加到现有的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睡美人的小仙女 2024-09-23 11:46:22

问题始于您的 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.

廻憶裏菂餘溫 2024-09-23 11:46:22

一半的麻烦是有问题的成员函数实际上没有与回调委托相同的签名。它本身需要一个函数指针。首先,您需要将 UnmanagedClass 更改为

class UnmanagedObject
{
public:
    UnmanagedObject() {} 
    int MethodD(int n) { return n; } 
};

我怀疑的 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

class UnmanagedObject
{
public:
    UnmanagedObject() {} 
    int MethodD(int n) { return n; } 
};

I suspect for the gcnew call that you simply have the parameters the wrong way around- i.e., mem func ptr, then obj.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文