Call C ++ code from a C # application or port?

Recently, I struggled with an algorithm that was poorly implemented (i.e. the developer was removed from another project and was unable to adequately document what he did) in C #.

I found an alternative (from numerical recipes) that works, but is written in C ++. So I think that probably the safest way to get something would be to wrap C ++ in a DLL.

Whereas, I'm still a little green when it comes to C #, and have never tried making a DLL from scratch, this sounds like a reasonable approach (and if so, has anyone tried this / got any advice) ? Or should I go all pigs and try to port the C ++ routine to C #?

Change . I am not looking for someone to make a decision for me, but if someone has any experience with any of the routes, I would be interested to hear their opinions and any unpleasant pitfalls that they should avoid. For example, how annoying is it to pass a C ++ STL vector in lists of data from C #?

+5
source share
4 answers

I tried to associate c-dll with C # code with good results, although I had problems sending data between environments. Otherwise, the procedure is quite simple. The more data you send back and forth (quantity and frequency), the slower your program will work, but you probably already understood it yourself.

, , - , - , # -c ( ).

, :

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();

    }
}
+4

# Mitch , ++-, C, , .Net. C API , API- .Net, .

+2

.

, .

#, C.

+1

... # .

+1
source

All Articles