<p>Has anybody out there used the <a href="http://www.swig.org/exec.html" rel="noreferrer">SWIG</a> 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.</p>
Edit:  Some clarification on target OS's.
<p>I plan on running the application on Linux and Windows, therefore the reason I am looking into SWIG.  P/Invoke is not an option.</p>

[{'answer_id': 26999, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 3, 'selected': False, 'text': '<p>I did attempt to use SWIG to wrap a project C++ for using in .NET a few years ago.</p>\n\n<p>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.</p>\n\n<p>We ended up using Managed C++ to wrap the C++ project. It worked really well.</p>\n\n<p>If you\'re just invoking functions straight out of a dll, I\'d suggest not worrying about either of the above, and just using <a href="http://msdn.microsoft.com/en-us/magazine/cc164123.aspx" rel="noreferrer">P/Invoke</a></p>\n'}, {'answer_id': 1067011, 'author': 'Roark Fan', 'author_id': 25362, 'author_profile': 'https://Stackoverflow.com/users/25362', 'pm_score': 4, 'selected': True, 'text': '<p>I think the mistake the earlier posters did was read the docs and not look at the examples. </p>\n\n<p>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 <code>Examples/csharp/class</code>, browsed the code, loaded the solution, grokked it, copied it, put in my code, it worked, my job was done. </p>\n\n<p>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). </p>\n\n<p>For my needs, it was simple and easy - I had <code>mystuff.dll</code>, and now in addition I can ship  <code>mystuffnet.dll</code>. I\'ll agree that the doc is difficult to get into.</p>\n\n<p>Edit: I noticed the OP only mentioned C. For that, you don\'t really need Swig, just use the <a href="http://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx#pinvoke_callingdllexport" rel="noreferrer">usual C#/C DLLImport interop syntax</a>. Swig becomes useful when you want to let <strong>C++ classes</strong> be invoked from C#.</p>\n'}, {'answer_id': 1070469, 'author': 'Marc Bernier', 'author_id': 23569, 'author_profile': 'https://Stackoverflow.com/users/23569', 'pm_score': 4, 'selected': False, 'text': '<p>For my last project, here\'s the entire C# SWIG configuration file:</p>\n\n<pre><code>%module mdProject\n\n%{\n#include "mdProject.h"\n%}\n</code></pre>\n\n<p>I compiled it in SWIG with:</p>\n\n<pre><code>swig -csharp -c++ -I../../Include mdProject.i\n</code></pre>\n\n<p>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.</p>\n\n<p>As @patrick mentioned, SWIG uses P/Invoke, so if you have a problem with that, you\'ll need to find another solution.</p>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/25268', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1117/']