views:

30

answers:

1

Does anyone know of a way to call MIT's Meep simulation package from C# (probably Mono, god help me).

We're stuck with the #$@%#$^ CTL front-end, which is a productivity killer. Some other apps that we're integrating into our sim pipeline are in C# (.NET). I've seen a Python interface to Meep (light years ahead of CTL), but I'd like to keep the code we're developing as homogeneous as possible.

And, no, writing the rest of the tools in Python isn't an option. Why? Because we hates it. Stupid Bagginses. We hates it forever!

(In reality, the various app targets don't lend themselves to a Python implementation, and the talent pool I have available is far more productive with C#.)

Or, in a more SO-friendly question form:

Is there a convenient/possible way to link GNU C++ libraries into C# on Windows or Mono on Linux?

A: 

The straightforward and portable solution is to write a C++ wrapper for libmeep that exposes a C ABI (via extern "C" { ... }), then write a C# wrapper around this API using P/Invoke. This would be roughly equivalent to the Python Meep wrapper, AFAICT.

Of course, mapping C++ classes to C# classes via a flat C API is nontrivial - you're going to have to keep IntPtr handles for the C++ classes in your C# classes, properly implement the Dispose pattern, using GCHandles or a dictionary of IntPtrs to allow referential integrity when resurfacing C++ objects (if needed), etc. Subclassing C++ objects in C# and being able to overriding virtual methods gets really quite complicated.

There is a tool called SWIG that can do this automatically but the results will not be anywhere near as good as a hand-written wrapper.

If you restrict yourself to Windows/.NET, Microsoft has a superset of C++ called C++/CLI, which would enable you to write a wrapper in C++ that exports a .NET API directly.

mhutch