结束语非托管C ++与C ++ / CLI - 适当的解决办法结束语、解决办法、适当、CLI

2023-09-02 21:37:14 作者:抹平你的忧愁

如题所述,我想有我的老C ++库中管理的.NET工作。我想到两个可能性:

as stated in the title, I want to have my old C++ library working in managed .NET. I think of two possibilities:

1)我可能会尝试使用/ clr编译库,并尝试它只是工作的做法。

1) I might try to compile the library with /clr and try "It Just Works" approach.

2)我可能会写一个托管包装的非托管库。

2) I might write a managed wrapper to the unmanaged library.

首先,我想有我的图书馆工作快,因为它是在非托管环境。因此,我不知道,如果第一种方法不会引起大的性能降低。然而,这似乎是更快地实现(而不是一个恰当的词:-))(假设它会为我工作)。

First of all, I want to have my library working FAST, as it was in unmanaged environment. Thus, I am not sure if the first approach will not cause a large decrease in performance. However, it seems to be faster to implement (not a right word :-)) (assuming it will work for me).

在另一方面,我觉得在写一个包装中可能出现的一些问题(例如,如何来包装一些STL集合(向量为例)?)我觉得写居住在同一项目中的非托管C ++驻留在包装的 - 是一种合理的方法(如 MyUnmanagedClass MyManagedClass 在同一个项目,第二个包裹除外)?

On the other hand, I think of some problems that might appear while writing a wrapper (e.g. how to wrap some STL collection (vector for instance)?) I think of writing a wrapper residing in the same project as the unmanaged C++ resides - is that a reasonable approach (e.g. MyUnmanagedClass and MyManagedClass in the same project, the second wrapping the other)?

你有什么建议在这个问题?哪种解决方案要给我所得到的code更好的性能?

What would you suggest in that problem? Which solution is going to give me better performance of the resulting code?

感谢您事先的任何建议和线索!

Thank you in advance for any suggestions and clues!

干杯

推荐答案

首先,忘掉托管C ++。使用C ++ / CLI。

First of all, forget about Managed C++. Use C++/CLI.

不同的是,托管C ++是在扩展C ++在.NET合作微软的第一次尝试,而且说实话,这是各种可怕的。

The difference is that Managed C++ was Microsoft's first attempt at extending C++ to work with .NET, and honestly, it was all kinds of horrible.

于是,他们放弃了这一点,设计的C ++ / CLI相反,它的效果要好得多。

So they gave up on that, and designed C++/CLI instead, which works much better.

二,有效的C ++ code应的只是工作的,如果你编译为C ++ / CLI,因此,它似乎是显而易见的方式来做到这一点。

Second, valid C++ code should just work if you compile it as C++/CLI, so that does seem like the obvious way to do it.

当然,为了暴露你的C ++类型.NET程序集,你将不得不写一些包装两种方式。对于STL类型,你可能会考虑微软的 STL / CLR 库。

Of course, in order to expose your C++ types to .NET assemblies, you're going to have to write some wrappers either way. For STL types, you might look into Microsoft's STL/CLR library.

但在一般情况下,只需添加/ CLI开关,编译code和C ++ / CLI,然后添加哪些封装​​协议,你所需要的。没有理由为什么你的code会神奇地变得更慢或任何东西。

But in general, just add the /cli switch, compile your code as C++/CLI, and then add what wrappers you need. There's no reason why your code would magically become slower or anything.