简体   繁体   English

C ++互操作CLI与PInvoke?

[英]C++ interop CLI vs PInvoke?

I believe lot of people already asked this question before, but i kept getting confused more and more. 我相信很多人以前已经问过这个问题,但是我越来越感到困惑。 I am looking for a answer in layman's terms. 我正在寻找外行人的答案。 I have a c++ library to perform one action. 我有一个c ++库来执行一个操作。 I need to call the functions from this library from my C# program. 我需要从我的C#程序中调用这个库中的函数。 What is better way to do this and why? 有什么更好的方法来做到这一点,为什么? whether pinvoke from C# app or write a wrapper in C++/CLI. 是否从C#app pinvoke或在C ++ / CLI中编写包装器。

There is only 1 function in C++ library (ReadNextRecord) which will be called from C# program. C ++库(ReadNextRecord)中只有1个函数,它将从C#程序中调用。 C# program should first create object of class defined in C++ library and then call a function to get the next record from a data source. C#程序应首先创建C ++库中定义的类的对象,然后调用一个函数以从数据源获取下一条记录。 Function is called many times ( >50000 times) so efficiency is an issue. 函数被多次调用(> 50000次),因此效率是个问题。

In most documents I see how to wrap a function to call from C# code. 在大多数文档中,我看到如何包装函数以从C#代码调用。 I dont see more complicated example where a c++ object is created in C# space and then a func is called on the object. 我没有看到更复杂的示例,其中在C#空间中创建了一个c ++对象,然后对该对象调用了func。

Please advice. 请指教。

Regards, Alok 此致,Alok

When dealing with C++ code, it is easier to use C++/CLI (at least, assuming you are working with the source code for the C++ library or at least know it is compiled with MSVC, C++ is not ABI-compatible across compilers in general). 在处理C ++代码时,使用C ++ / CLI更容易(至少,假设您正在使用C ++库的源代码,或者至少知道它是使用MSVC编译的,C ++在编译器之间通常不兼容ABI) )。 PInvoke is mostly useful for C functions, but can be made to work with some C++ functions (see this question ). PInvoke主要用于C函数,但可以用于某些C ++函数(请参阅此问题 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM