如何实现从非托管DLL到.net应用程序的回调接口?
在我的下一个项目中,我想为 C++ 中已有的代码实现一个 GUI。 我的计划是将 C++ 部分包装在 DLL 中,并用 C# 实现 GUI。我的问题是我不知道如何实现从非托管 DLL 到托管 C# 代码的回调。我已经在 C# 中完成了一些开发,但托管代码和非托管代码之间的接口对我来说是新的。有人可以给我一些提示或阅读技巧或一个简单的例子吗?不幸的是我找不到任何有用的东西。
in my next project I want to implement a GUI for already existing code in C++.
My plan is to wrap the C++ part in a DLL and to implement the GUI in C#. My problem is that I don't know how to implement a callback from the unmanaged DLL into the manged C# code. I've already done some development in C# but the interfacing between managed and unmanaged code is new to me. Can anybody give me some hints or reading tips or a simple example to start from? Unfortunatly I could not find anything helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要使用 Marshal.GetFunctionPointerForDelegate(),P/Invoke 编组器会自动执行此操作。您需要在 C# 端声明一个委托,其签名与 C++ 端的函数指针声明兼容。例如:
以及用于创建非托管 DLL 的相应 C++ 代码:
这足以让您开始使用它。一百万个细节可能会给你带来麻烦,你一定会遇到其中的一些。运行此类代码的更高效方法是使用 C++/CLI 语言编写包装器。这还允许您包装 C++ 类,这是 P/Invoke 无法做到的。 此处提供了一个不错的教程。
You don't need to use Marshal.GetFunctionPointerForDelegate(), the P/Invoke marshaller does it automatically. You'll need to declare a delegate on the C# side whose signature is compatible with the function pointer declaration on the C++ side. For example:
And the corresponding C++ code used to create the unmanaged DLL:
That's enough to get you started with it. There are a million details that can get you into trouble, you are bound to run into some of them. The much more productive way to get this kind of code going is writing a wrapper in the C++/CLI language. That also lets you wrap a C++ class, something you can't do with P/Invoke. A decent tutorial is available here.
P/Invoke 可以处理将托管委托编组到函数指针的情况。因此,如果您公开从 DLL 注册回调函数的 API,并在 C# 中将委托传递给该函数。
MSDN 上有一个执行此操作的示例使用 EnumWindows 函数。在那篇文章中,请注意第 4 点中的内容:
这就是说,您需要确保您的委托在托管代码完成调用它之前不会被垃圾收集,方法是在代码中保留对它的引用或固定它。
P/Invoke can handle marshaling a managed delegate to a function pointer. So if you expose API's that register a call back function from your DLL and in C# pass a delegate to that function.
There is an example on MSDN of doing this with the EnumWindows function. In that article be careful to pay attention to the line in point 4 that states:
What that is saying is that you need to make sure that your delegate isn't garbage collected until after the managed code is done calling it by either keeping a reference to it in your code, or pinning it.
请参阅 Marshal.GetFunctionPointerForDelegate,其中将为您提供一个函数指针,用于从非托管代码调用托管(即 C# 代码)。
See Marshal.GetFunctionPointerForDelegate, which will give you a function pointer for calling managed (i.e. C# code) from unmanaged code.
看看这个, Marshal.GetDelegateForFunctionPointer< /a>?
Have a look at this, Marshal.GetDelegateForFunctionPointer?