从C#应用程序或端口调用C ++代码吗?

时间:2021-07-21 04:47:45

I've recently been wrestling with an algorithm which was badly implemented (i.e. the developer was pulled off onto another project and failed to adequately document what he'd done) in C#.

我最近一直在努力解决一个在C#中实施得很糟糕的算法(即开发人员被拉到另一个项目并且未能充分记录他所做的事情)。

I've found an alternative (from numerical recipes) which works but is written in C++. So I'm thinking probably the safest way to get something working would be to wrap the C++ up in a DLL.

我找到了一个替代方案(来自数字配方),但它是用C ++编写的。因此,我认为最安全的方法是将C ++包装在DLL中。

Bearing in mind that I'm still a bit green when it comes to C# and have never tried making a DLL from scratch, does this sound like a reasonable approach (and if so, has anyone tried this / got any advice)? Or should I go the whole hog and try and port the C++ routine into C#?

请记住,在C#方面我仍然有点绿,并且从未尝试过从头开始创建DLL,这听起来像是一种合理的方法(如果有的话,有没有人试过这个/得到任何建议)?或者我应该全力以赴并尝试将C ++例程移植到C#中?

Edit - I'm not looking for anyone to make the decision for me, but if anyone has any exprience of either route I'd be interested to hear their opinions and any nasty pitfalls that should be avoided. For example, how nasty is passing in lists of data from C# to a C++ STL vector?

编辑 - 我不是在寻找任何人为我做出决定,但如果有人对任何一条路线都有任何表现,我会有兴趣听取他们的意见和任何应该避免的令人讨厌的陷阱。例如,从C#到C ++ STL向量的数据列表传递是多么令人讨厌?

4 个解决方案

#1


I tried linking to c-dll's from c# code with quite good results, even though I had some problems sending data between the environments. Otherwise the procedure is quite straight forward. The more data you send back and forth (both amount and frequency) the slower your program will run, but you have probably already figured this out on your own.

我尝试从c#代码链接到c-dll,结果非常好,即使我在环境之间发送数据时遇到了一些问题。否则,程序非常简单。你来回发送的数据越多(数量和频率)你的程序运行得越慢,但你可能已经自己想出了这个。

The main drawback was maintaining the c#-c glue code (interface code) each time something changed or someone found a bug.

主要缺点是每次更改或有人发现错误时都要维护c#-c胶代码(接口代码)。

Here is a bit of code to get you started:

这里有一些代码可以帮助您入门:

using System.Runtime.InteropServices;
    class myDllCaller {

       //call to function in the dll returning an int
      [DllImport("MyFavorite.dll")]
      private static extern int dllFunction(//list of parameters to function);

    public static void Main() {
    int rerult = dllFunction();

    }
}

#2


If the C# version Mitch references isn't of a suitable licence for your purposes, You could use a managed C++ wrapper, that could reuse, and wrap the C code you have, but still be visible to your managed applications as a native .Net assembly. I've used this approach in the past for using the C API of a library that didn't have its own native .Net API, and found it relatively painless marshalling data between the two.

如果C#版Mitch引用不适合您的目的,您可以使用托管C ++包装器,它可以重用,并包装您拥有的C代码,但仍然可以作为本机.Net显示您的托管应用程序。部件。我过去曾使用过这种方法来使用没有自己的原生.Net API的库的C API,并发现两者之间的编组数据相对无痛。

#3


...Or you could download the already implemented code in C# from here.

...或者您可以从此处下载已在C#中实现的代码。

#4


It depends what your goals are.

这取决于你的目标。

If it's to have a working application I'd weigh-up the costs and benefits of both approaches and go with the most cost effective.

如果要有一个有效的应用程序,我会权衡这两种方法的成本和收益,并以最具成本效益的方式进行。

If it's to improve your C# then by all means rewrite the C.

如果要改进你的C#那么一定要重写C.

#1


I tried linking to c-dll's from c# code with quite good results, even though I had some problems sending data between the environments. Otherwise the procedure is quite straight forward. The more data you send back and forth (both amount and frequency) the slower your program will run, but you have probably already figured this out on your own.

我尝试从c#代码链接到c-dll,结果非常好,即使我在环境之间发送数据时遇到了一些问题。否则,程序非常简单。你来回发送的数据越多(数量和频率)你的程序运行得越慢,但你可能已经自己想出了这个。

The main drawback was maintaining the c#-c glue code (interface code) each time something changed or someone found a bug.

主要缺点是每次更改或有人发现错误时都要维护c#-c胶代码(接口代码)。

Here is a bit of code to get you started:

这里有一些代码可以帮助您入门:

using System.Runtime.InteropServices;
    class myDllCaller {

       //call to function in the dll returning an int
      [DllImport("MyFavorite.dll")]
      private static extern int dllFunction(//list of parameters to function);

    public static void Main() {
    int rerult = dllFunction();

    }
}

#2


If the C# version Mitch references isn't of a suitable licence for your purposes, You could use a managed C++ wrapper, that could reuse, and wrap the C code you have, but still be visible to your managed applications as a native .Net assembly. I've used this approach in the past for using the C API of a library that didn't have its own native .Net API, and found it relatively painless marshalling data between the two.

如果C#版Mitch引用不适合您的目的,您可以使用托管C ++包装器,它可以重用,并包装您拥有的C代码,但仍然可以作为本机.Net显示您的托管应用程序。部件。我过去曾使用过这种方法来使用没有自己的原生.Net API的库的C API,并发现两者之间的编组数据相对无痛。

#3


...Or you could download the already implemented code in C# from here.

...或者您可以从此处下载已在C#中实现的代码。

#4


It depends what your goals are.

这取决于你的目标。

If it's to have a working application I'd weigh-up the costs and benefits of both approaches and go with the most cost effective.

如果要有一个有效的应用程序,我会权衡这两种方法的成本和收益,并以最具成本效益的方式进行。

If it's to improve your C# then by all means rewrite the C.

如果要改进你的C#那么一定要重写C.