Create C # bindings for a complex C ++ class system?

I have an existing C ++ lib containing many different classes working together. In some examples, use should include something like passing an instance of one class to the constructor / method of another class.

I plan to provide a C # binding for these C ++ classes using the C ++ / CLI, so I don't need to port all the code to C ++.

I can already do this in Facade by creating another class that hides all the classes used in existing C ++ code from User. However, I want to provide users with the same classes with the same method signatures.

Are there any recommendations or recommendations for this?

ps. I reviewed some of the existing projects linking open source C # to C ++. But they seem to have used many different ways to do this, and I really don't understand.

+4
source share
2 answers

Much will depend on the factoring of your classes.

In the work that I am doing, I am trying to consider C ++ models that I model as hidden implementation details that I transfer to the corresponding C ++ / CLI classes. For the most part, I can do without this by managing interfaces that are NOT particularly granular. When your implementation involves the direct execution of every detail of the underlying C ++ code, then you will get a very "chat" interface, which will include significant cost in managed / unmanaged transitions.

In particular, if your unmanaged C ++ classes use stl, especially stl collection types, you will probably come across an unpleasant surprise when you find that each iteration through your stl collections includes several managed / unmanaged transitions. Because of this, I had an image decoder that used stl, and because of this, it ran like a dog. The obvious fix for #pragmas regarding the code accessed by the stl types did not help. I found that it worked to hide it all on the C interface on the handle that hid all C ++ - isms behind the iron curtain. No stl opened anywhere means that it is allowed to exist as unmanaged code.

I think your biggest problem will be how you process collections (if you use them), since the C ++ collection philosophy and the .NET collection philosophy are not consistent. I suspect that you will spend a lot of time comparing .NET collections of adapted classes with your C ++ sets / types.

EDIT Here is a blog article I wrote about this question a while ago. It uses the managed C ++ dialect, not the C ++ / CLI, but the problem is the same.

+5
source

I once linked C ++ to C # using only the [DllImport] attribute. If you have any of the STL releases that you complained about here and your library is fairly simple (like a single DLL, for example), I assume this is the easiest way to bind C ++ and C #.

A simple example on MSDN: http://msdn.microsoft.com/en-us/library/aa984739(VS.71).aspx

+1
source

Source: https://habr.com/ru/post/1302748/


All Articles