C ++和C#互操作:P / Invoke的VS C ++ / CLI [英] C++ and C# interoperability : P/Invoke vs C++/CLI

查看:560
本文介绍了C ++和C#互操作:P / Invoke的VS C ++ / CLI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻找一种方法来C#和C之间互操作的过程中++,我发现这个文章解释有关的P / Invoke。

In the course of finding a way to interoperate between C# and C++ I found this article that explains about P/Invoke.

和我读了很多文章声称C ++ / CLI不准确C ++和需要一些努力,从最初的C ++代码进行修改。

And I read a lot of articles claiming that C++/CLI is not exact C++ and requires some effort to modify from original C++ code.

我要问什么是最佳的方式,当我有我想从C#对象使用一些C ++对象(代码/数据)。

I want to ask what would be the optimal way when I have some C++ objects (code/data) that I want to use from C# objects.


  • 看起来,为了使用的P / Invoke,我应该提供C风格的API。是真的吗?我的意思是,有没有C ++对象导出到C#像SWIG以P的方法/调用?或者,我必须使用痛饮此目的?

  • 有多难改变C ++到C ++ / CLI?它是值得尝试相比,重写C ++到C#? C ++的精心设计,所以它实现到C#是不是很大。

  • (关题目问题)是否有另外的方法?我的意思是,如果我想从C ++使用C#代码,有没有办法这样做呢?

推荐答案

我不建议你rewritng C ++库为C ++ / CLI。相反,我会写一个C ++ / CLI的包装,你可以从C#调用。这将包括一些公开引用类类,其中每个可能只是管理本机类的一个实例。你的C ++ / CLI包装只是包含标题,链接到lib要使用本机库。因为你已经写了公开引用类类,C#代码只是增加了一个.NET参考。和所有你的每个公共引用类里面做的是使用C ++的Interop(又名这只是工作互操作)来调用本机代码。而你在它,如果你愿意的话,可以申请外观。

I would not recommend rewritng your C++ library into C++/CLI. Instead, I would write a C++/CLI wrapper that you can call from C#. This would consist of some public ref class classes, each of which probably just manages an instance of the native class. Your C++/CLI wrapper just "include the header, link to the lib" to use the native library. Because you have written public ref class classes, your C# code just adds a .NET reference. And all you do inside each public ref class is use C++ Interop (aka It Just Works interop) to call the native code. You can apply a facade while you're at it if you like.

这篇关于C ++和C#互操作:P / Invoke的VS C ++ / CLI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆