C++/CLI wrapping a C library: cheatsheet

Because once in a while I write a C++/CLI wrapper around a C library, but since I do it so infrequently I forget everything each time. Here is a list of pointers (haha) for common pitfalls and problems.

Compiling and debugging

You have to wrap the .h into an #ifdef __cplusplus extern “C” :

You also need to remove the /clr option for C files.
Right click your C files > Properties > C/C++ > General > Common Language RunTime Support > No CLR Support.

In your managed class, to include an unmanaged C header, use managed(push|pop)

Because Linux and Windows get along so well, you may also have to redefine a few C methods in your header file.

If you wish to create a C# unit test project (using xUnit for instance), with Visual Studio 2012 and later, you will have to do a few things:

  • In the C++/CLI project’s properties, in Debugging, choose the Debugger Type “Mixed”
  • In the Visual Studio options, Debugging, check “Managed C++ Compatibility Mode” (in VS2012) / “Use Managed Compatibility Mode” (in VS2013).

To build using MSBuild, you may have to change the toolset to VS2012: project properties > General > Platform Toolset = Visual Studio 2012 (v110). And set the Configuration Type to Dynamic Library (.dll) while you’re at it.

Managed and unmanaged objects

gcnew instantiates a managed objects, which will be garbage collected. Always use it for the managed objects.

To easily call the native structures through managed classes, use the internal constructor and ToNative methods.