Take the 2-minute tour ×

Has anybody out there used the SWIG library with C#? If you have, what pitfalls did you find and what is the best way to use the library? I am thinking about using it as a wrapper for a program that was written in C and I want to wrap the header files where I can use them in my .NET application.

Edit: Some clarification on target OS's.

I plan on running the application on Linux and Windows, therefore the reason I am looking into SWIG. P/Invoke is not an option.

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

I think the mistake the earlier posters did was read the docs and not look at the examples.

A few hours ago I needed to interface some C++ classes to C#. I looked in my Swig dir (I already had it for other work), found the directory Examples/csharp/class, browsed the code, loaded the solution, grokked it, copied it, put in my code, it worked, my job was done.

With that said, generated P/Invoke code isn't a solution for all needs. Depending on your project, it may be just as simple to write some simple API wrappers yourself or write managed C++ (Look up SlimDX for a superb example of this).

For my needs, it was simple and easy - I had mystuff.dll, and now in addition I can ship mystuffnet.dll. I'll agree that the doc is difficult to get into.

Edit: I noticed the OP only mentioned C. For that, you don't really need Swig, just use the usual C#/C DLLImport interop syntax. Swig becomes useful when you want to let C++ classes be invoked from C#.

share|improve this answer
I think SWIG is cool even for C. You can turn procedural programming into oop! –  Matthias Dec 23 '11 at 12:56
add comment

For my last project, here's the entire C# SWIG configuration file:

%module mdProject

#include "mdProject.h"

I compiled it in SWIG with:

swig -csharp -c++ -I../../Include mdProject.i

This generated a Project.cxx which I compiled and linked directly into the 'main' DLL, so I didn't need a second C++ 'helper' DLL. SWIG also generated a bunch of C# files which I compiled into a .NET DLL. My other wrappers (Java, PHP, etc) do use a helper DLL.

As @patrick mentioned, SWIG uses P/Invoke, so if you have a problem with that, you'll need to find another solution.

If you use types that stray from the ordinary (voids, structures, etc), you will have to do some extra work to get it right, but for the average API using int's, char*'s etc, it's fine.

share|improve this answer
I know this was last year.. but would you be able to share how you "compiled and linked directly into the 'main' DLL" the generated .cxx file? I'm completely clueless on how you do this. Thanks in advance! :) –  Ozzie Perez Nov 26 '10 at 7:46
You would need to have source code to 'main' DLL (C++ in my case). I just opened the DLLs C++ project, added SWIG's auto-generated C++ file to the project and re-built. If you don't have access to the DLL's source, you can't do this. In other words, you can't incrementally add a new source file to an existing DLL - the whole thing must be built from scratch again with the addition of the new source file. Many times you don't have access to the source, so you end up having to make a little 'go-between' DLL between your C# code and the DLL. –  Marc Bernier Nov 29 '10 at 15:35
add comment

I did attempt to use SWIG to wrap a project C++ for using in .NET a few years ago.

I didn't get very far as it was a massive giant pain to produce the configuration that SWIG required. At the time I just wanted a solution, not to learn another language/api/etc. SWIG may be easier to use these days, I couldn't tell you.

We ended up using Managed C++ to wrap the C++ project. It worked really well.

If you're just invoking functions straight out of a dll, I'd suggest not worrying about either of the above, and just using P/Invoke

share|improve this answer
add comment

its kind of difficult, its probably not worth the effort. Go read the section on typemaps for example, and see if that makes a lot of sense. Think harder about what you want, there's probably an easier way.

Swig generates P/Invoke wrappers, so I'm not real clear how P/Invoke is not an option for you.

share|improve this answer
add comment

Your Answer


