调用从非托管C ++ C#的功能(通过托管包装) [英] Calling a C# function from unmanaged c++ (via a managed wrapper)
问题描述
我有C ++源&放大器;标头一组,我需要从C#应用程序调用的库。我创建围绕我需要和我能够从C#调用它们向后和向前编组的数据功能的托管C ++包装。
现在最困难的部分。<因为它运行,我希望能够从调用C#应用程序中显示这些/ p>
我的非托管C ++库生成状态消息。我目前的想法是这样的:
我想非托管C ++库代码来调用我的C#代码的函数,我传递给托管包装,因为我创造它。我发现一些教程代码项目,但语法似乎是过时了。
如果任何人有一些示例代码或可能指向我的方向很好的教程,这将是巨大的。
在事先的任何帮助感谢。
您可以通过一个.NET委托到C ++ / CLI函数,它指向一个功能是匹配的论点。
注意事项
- 的指针到函数必须STDCALL调用约定
- 如果委托是一个对象的成员,这个函数指针不会算作一个参考,以保持对象存活。你必须在时间内保持对对象的引用回调举行
既然你认为你的例子是过时的,我会假设你正在使用C ++ / CLI的新语法。这里是一个CodeProject上对如何做到这一点。
的 http://www.codeproject.com/KB/mcpp/FuncPtrDelegate.aspx
I have C++ source & headers for a set of libraries which I need to call from a C# application. I've created a managed C++ wrapper around the functions I need and am able to call them from C# marshalling the data backwards and forwards.
Now the hard part..
My unmanaged C++ library generates status messages as it runs and I'd like to be able to display these from the calling C# application. My current thinking goes like this:
I'd like the unmanaged C++ library code to call a function in my C# code that I pass to the managed wrapper as I create it. I've found a few tutorials on Code Project but the syntax seem to be out of date.
If anyone has some sample code or could point me in the direction of a good tutorial that would be great.
Thanks in advance for any help.
You can pass a .NET delegate to a C++/CLI function that takes a pointer to a function with "matching" arguments.
Caveats
- The pointer-to-function must be STDCALL calling conventions
- If the delegate is a member of an object, this pointer to function will not count as a reference to keep the object alive. You have to maintain a reference to the object during the time that the callback is held
Since you think your examples are out of date, I am going to assume you are using the new syntax of C++/CLI. Here is a CodeProject with an example of how to do that
http://www.codeproject.com/KB/mcpp/FuncPtrDelegate.aspx
这篇关于调用从非托管C ++ C#的功能(通过托管包装)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!