Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ /CLI Automatic Wrapper Generator

In our project we have a significant amount of native C++ code which we'd like to re-use from a brand new UI layer that is to be written in .Net.

I'd like to use C++/CLI for this matter and I've seen that there is a pretty straight-forward and rather technical way to do it by wrapping native classes with thin managed classes. I'd expect to be able to find a tool which automates this task since it seems to me really as a technical issue without a lot of required "human" thinking.

There is such tool which uses explicit p/invoke for this - see http://www.swig.org/Doc1.3/CSharp.html. The thing is that explicit p/invoke is more cumbersome and less efficient in performance.

Is there anyone out there who is familiar with such tool?

Is there an inherent reason why such tool is not available yet?

like image 534
Elad Avatar asked Dec 24 '12 13:12

Elad


People also ask

How to create C++/CLI project in Visual Studio?

To create a C++/CLI project in Visual StudioIn Solution Explorer, right-click on the top to open the Create a New Project dialog box. At the top of the dialog, type CLR in the search box and then choose CLR Empty Project from the results list. Choose the Create button to create the project.

What does C++/CLI stand for?

C++/CLI is variant of the C++ programming language, modified for Common Language Infrastructure. It has been part of Visual Studio 2005 and later, and provides interoperability with other . NET languages such as C#. Microsoft created C++/CLI to supersede Managed Extensions for C++.

What is C++ CLI wrapper?

Creating a C++/CLI Wrapper. The C++/CLI is a dialect of C++ that is designed to work with the Common Language Infrastructure (CLI). It is a replacement for 'Managed C++' and makes every feature of the CLI easily accessible from C++.

How do I call C++ code from C#?

If you want to use C++ in c# code directly, you can create a CLR(C++/CLI) project, then you can write c++ and c# code in the same project.


1 Answers

The reason p/invoke is less efficient is because it has to be a very generic interface to any function, knowing only the signature. Your hypothetical wrapper generator will perform no better.

On the other hand, the developer coding a custom wrapper class has a lot more information about functions that get used together and so on, and can therefore avoid a lot of slow and unnecessary conversions, for example by storing values directly inside the wrapper and not converting them to some .NET-friendly format.

So it's not that such a tool is impossible, there's just little-to-no benefit to it. If you care about performance, you write your own wrapper, complete with lifetime-management smart pointers and the like.

Of course, p/invoke only provides access to bare functions. That's where SWiG should be helpful, to expose any C++ class interfaces via bare exported functions.

I did find one lead on a tool that might be what you're looking for, although it sounds pretty narrow in focus:

  • http://www.ogre3d.org/addonforums/viewtopic.php?f=8&t=4071
  • example output: http://extramegablob.googlecode.com/svn/trunk/MOIS/MOIS/include/auto/MOISEffect.h
like image 180
Ben Voigt Avatar answered Sep 18 '22 12:09

Ben Voigt