<p>I would like to call my unmanaged C++ libraries from my C# code. What are the potential pitfalls and precautions that need to be taken? Thank you for your time.</p>

[{'answer_id': 76696, 'author': 'Jesse C. Slicer', 'author_id': 3312, 'author_profile': 'https://Stackoverflow.com/users/3312', 'pm_score': 2, 'selected': False, 'text': '<p>There are a couple routes you can go with this - one, you can update your unmanaged C++ libraries to have a managed C++ extensions wrapper around them and have C# utilize those classes directly.  This is a bit time-consuming, but it provides a nice bridge to legacy unmanaged code.  But be aware that managed C++ extensions are sometimes a bit hard to navigate themselves as the syntax is similar to unmanaged C++, but close enough that a very trained eye will be able to see the differences.</p>\n\n<p>The other route to go is have your umnanaged C++ implement COM classes and have C# utilize it via an autogenerated interop assembly.  This way is easier if you know your way around COM well enough.</p>\n\n<p>Hope this helps.</p>\n'}, {'answer_id': 76775, 'author': '1800 INFORMATION', 'author_id': 3146, 'author_profile': 'https://Stackoverflow.com/users/3146', 'pm_score': 0, 'selected': False, 'text': '<p>You can also call into unmanaged code via P/Invoke. This may be easier if your code doesn\'t currently use COM. I guess you would probably need to write some specific export points in your code using "C" bindings if you went this route.</p>\n\n<p>Probably the biggest thing you have to watch out for in my experience is that the lack of deterministic garbage collection means that your destructors will not run when you might have thought they would previously. You need to keep this in mind and use IDisposable or some other method to make sure your managed code is cleaned up when you want it to be.</p>\n'}, {'answer_id': 76787, 'author': 'DougN', 'author_id': 7442, 'author_profile': 'https://Stackoverflow.com/users/7442', 'pm_score': 1, 'selected': False, 'text': "<p>You're describing P/Invoke.  That means your C++ library will need to expose itself via a DLL interface, and the interface will need to be simple enough to describe to P/Invoke via the call attributes.  When the managed code calls into the unmanaged world, the parameters have to be marshalled, so it seems there could be a slight performance hit, but you'd have to do some testing to see if the marshalling is significant or not.</p>\n"}, {'answer_id': 76837, 'author': 'stephbu', 'author_id': 12702, 'author_profile': 'https://Stackoverflow.com/users/12702', 'pm_score': 0, 'selected': False, 'text': '<p>Of course there is always PInvoke out there too if you packaged your code as DLLs with external entrypoints.  None of the options are pain free.  They depend on either a) your skill at writing COM or Managed C wrappers b) chancing your arm at PInvoke.  </p>\n'}, {'answer_id': 76856, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>The easiest way to start is to make sure that all the C++ functionality is exposed as \'C\' style functions.  Make sure to declare the function as _stdcall.</p>\n\n<p>extern "C" __declspec(dllexport) int _stdcall Foo(int a)</p>\n\n<p>Make sure you get the marshalling right, especially things like pointers &amp; wchar_t *.  If you get it wrong, it can be difficult to debug.</p>\n\n<p>Debug it from either side, but not both.  When debugging mixed native &amp; managed, the debugger can get very slow.  Debugging 1 side at a time saves lots of time.</p>\n\n<p>Getting more specific would require a more specific question.</p>\n'}, {'answer_id': 77024, 'author': 'Alan', 'author_id': 2958, 'author_profile': 'https://Stackoverflow.com/users/2958', 'pm_score': 0, 'selected': False, 'text': '<p>I would take a look at <a href="http://www.swig.org/" rel="nofollow noreferrer">swig</a>, we use this to good effect on our project to expose our C++ API to other language platforms.  </p>\n\n<p>It\'s a well maintained project that effectively builds a thin wrapper around your C++ library that can allow languages such as C# to communicate directly with your native code - saving you the trouble of having to implement (and debug) glue code.</p>\n'}, {'answer_id': 77176, 'author': 'Alex Reitbort', 'author_id': 11525, 'author_profile': 'https://Stackoverflow.com/users/11525', 'pm_score': 0, 'selected': False, 'text': '<p>If you want a good PInvoke examples you can look at <a href="http://www.pinvoke.net/" rel="nofollow noreferrer">PInvoke.net</a>. It has examples of how to call most of win api functions.</p>\n\n<p>Also you can use tool from this article <a href="http://msdn.microsoft.com/en-us/magazine/cc164193.aspx" rel="nofollow noreferrer">Clr Inside Out: PInvoke</a> that will translate your .h file to c# wrappers.</p>\n'}, {'answer_id': 77348, 'author': 'Michael Burr', 'author_id': 12711, 'author_profile': 'https://Stackoverflow.com/users/12711', 'pm_score': 2, 'selected': True, 'text': '<p>This question is too broad.  The only reasonable answer is P/Invoke, but that\'s kind of like saying that if you want to program for Windows you need to know the Win32 API.</p>\n\n<p>Pretty much entire books have been written about P/Invoke (<a href="https://rads.stackoverflow.com/amzn/click/com/067232170X" rel="nofollow noreferrer" rel="nofollow noreferrer">http://www.amazon.com/NET-COM-Complete-Interoperability-Guide/dp/067232170X</a>), and of course entire websites have been made: <a href="http://www.pinvoke.net/" rel="nofollow noreferrer">http://www.pinvoke.net/</a>.</p>\n'}]
['https://Stackoverflow.com/questions/76629', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13673/']